posts - 41,  comments - 40,  trackbacks - 0

          剛剛寫的看誰復(fù)制的快,只是由于在項(xiàng)目中猶豫到底是用哪個好而寫的,沒想到大家很感興趣,那我再把讀取文件誰快也翻上來,有錯盡管拍磚。

          另外,最好能放在有上萬張10KB以上的圖片的文件夾下運(yùn)行,否則不一定看出效果,我的是六千多張,10240輕松取勝。

          import java.io.File;
          import java.io.IOException;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;


          /*******************************************************************************
          ?*
          ?*
          ?* Author: NeedJava
          ?*
          ?* Modified: 2007.08.26
          ?*
          ?******************************************************************************/
          public final class ReadFaster
          {
          ? /*****************************************************************************
          ?? *
          ?? * 構(gòu)造函數(shù),默認(rèn)使用當(dāng)前路徑
          ?? *
          ?? ****************************************************************************/
          ? public ReadFaster()
          ? {
          ??? this( "." );
          ? }

          ? public ReadFaster( String fileName )
          ? {
          ??? this.listPictures( null, fileName );
          ? }


          ? /*****************************************************************************
          ?? *
          ?? * 列出當(dāng)前目錄下的文件列表,包括文件和文件夾
          ?? *
          ?? ****************************************************************************/
          ? private final void listPictures( File path, String fileName )
          ? {
          ??? File file=new File( path, fileName );

          ??? if( file.isDirectory() )
          ????? {
          ??????? //得到當(dāng)前目錄下的文件列表,包括文件和文件夾
          ??????? String[] children=file.list();

          ??????? //如果子集為空,就放棄后面的操作
          ??????? if( children==null )
          ????????? {
          ??????????? return;
          ????????? }

          ??????? //排序
          ??????? //java.util.Arrays.sort( children );

          ??????? //如果子集不為空,則顯示
          ??????? for( int i=0; i<children.length; i++ )
          ?????????? {
          ???????????? listPictures( file, children[i] );
          ?????????? }
          ????? }
          ??? else if( file.isFile() )
          ?????????? {
          ???????????? if( isPictureSuffix( file.getPath() ) )
          ?????????????? {
          ???????????????? readPicture( file );
          ?????????????? }
          ?????????? }
          ? }


          ? /*****************************************************************************
          ?? *
          ?? * 根據(jù)后綴名判斷是否是有效的圖片,并且返回小寫的后綴名
          ?? *
          ?? ****************************************************************************/
          ? private final boolean isPictureSuffix( String fileName )
          ? {
          ??? if( fileName==null )
          ????? {
          ??????? return false;
          ????? }

          ??? int length=fileName.length();

          ??? //可能存在“.jpg”這樣的文件,即4個字符
          ??? if( length>=4 )
          ????? {
          ??????? char c=fileName.charAt( length-4 );

          ??????? if( c=='.' )
          ????????? {
          ??????????? c=fileName.charAt( length-3 );

          ??????????? if( c=='j'||c=='J' )
          ????????????? {
          ??????????????? c=fileName.charAt( length-2 );

          ??????????????? if( c=='p'||c=='P' )
          ????????????????? {
          ??????????????????? c=fileName.charAt( length-1 );

          ??????????????????? if( c=='g'||c=='G' )
          ????????????????????? {
          ??????????????????????? return true;
          ????????????????????? }
          ??????????????????? else if( c=='e'||c=='E' )
          ?????????????????????????? {
          ???????????????????????????? return true;
          ?????????????????????????? }
          ????????????????? }
          ????????????? }
          ??????????? else if( c=='t'||c=='T' )
          ?????????????????? {
          ???????????????????? c=fileName.charAt( length-2 );

          ???????????????????? if( c=='i'||c=='I' )
          ?????????????????????? {
          ???????????????????????? c=fileName.charAt( length-1 );

          ???????????????????????? if( c=='f'||c=='F' )
          ?????????????????????????? {
          ???????????????????????????? return true;
          ?????????????????????????? }
          ?????????????????????? }
          ?????????????????? }
          ????????? }
          ??????? else if( c=='j'||c=='J' )
          ?????????????? {
          ???????????????? c=fileName.charAt( length-3 );

          ???????????????? if( c=='p'||c=='P' )
          ?????????????????? {
          ???????????????????? c=fileName.charAt( length-2 );

          ???????????????????? if( c=='e'||c=='E' )
          ?????????????????????? {
          ???????????????????????? c=fileName.charAt( length-1 );

          ???????????????????????? if( c=='g'||c=='G' )
          ?????????????????????????? {
          ???????????????????????????? return true;
          ?????????????????????????? }
          ?????????????????????? }
          ?????????????????? }
          ???????????????? else if( c=='f'||c=='F' )
          ??????????????????????? {
          ????????????????????????? c=fileName.charAt( length-2 );

          ????????????????????????? if( c=='i'||c=='I' )
          ??????????????????????????? {
          ????????????????????????????? c=fileName.charAt( length-1 );

          ????????????????????????????? if( c=='f'||c=='F' )
          ??????????????????????????????? {
          ????????????????????????????????? return true;
          ??????????????????????????????? }
          ??????????????????????????? }
          ??????????????????????? }
          ?????????????? }
          ??????? else if( c=='t'||c=='T' )
          ?????????????? {
          ???????????????? c=fileName.charAt( length-3 );

          ???????????????? if( c=='i'||c=='I' )
          ?????????????????? {
          ???????????????????? c=fileName.charAt( length-2 );

          ???????????????????? if( c=='f'||c=='F' )
          ?????????????????????? {
          ???????????????????????? c=fileName.charAt( length-1 );

          ???????????????????????? if( c=='f'||c=='F' )
          ?????????????????????????? {
          ???????????????????????????? return true;
          ?????????????????????????? }
          ?????????????????????? }
          ?????????????????? }

          ?????????????? }
          ????? }

          ??? return false;
          ? }


          ? /*****************************************************************************
          ?? *
          ?? * 大于10240的,每次讀1024或2048
          ?? *
          ?? * 小于10240的,讀10240一次即可
          ?? *
          ?? ****************************************************************************/
          ? private final String readPicture( File file )
          ? {
          ??? try{ FileInputStream fis=new FileInputStream( file );

          ???????? //小于10K的忽略
          ???????? if( fis.available()<10240 )
          ?????????? {
          ???????????? return "";
          ?????????? }

          ???????? long num=0L;

          ???????? //Buffered的默認(rèn)有2048和8192

          ???????? //*/ No.1
          ???????? byte[] buffer=new byte[10240];

          ???????? if( fis.read( buffer )==10240 )
          ?????????? {
          ???????????? for( int i=0; i<10240; i++ )
          ??????????????? {
          ????????????????? num++;
          ??????????????? }
          ?????????? }
          ???????? //*/

          ???????? /*/ No.3
          ???????? byte[] buffer=new byte[5120];

          ???????? for( int j=0; j<2; j++ )
          ??????????? {
          ????????????? if( fis.read( buffer )==5120 )
          ??????????????? {
          ????????????????? for( int i=0; i<5120; i++ )
          ???????????????????? {
          ?????????????????????? num++;
          ???????????????????? }
          ??????????????? }
          ??????????? }
          ???????? //*/

          ???????? /*/ No.2
          ???????? byte[] buffer=new byte[2048];

          ???????? for( int j=0; j<5; j++ )
          ??????????? {
          ????????????? if( fis.read( buffer )==2048 )
          ??????????????? {
          ????????????????? for( int i=0; i<2048; i++ )
          ???????????????????? {
          ?????????????????????? num++;
          ???????????????????? }
          ??????????????? }
          ??????????? }
          ???????? //*/

          ???????? /*/ No.4
          ???????? byte[] buffer=new byte[1024];

          ???????? for( int j=0; j<10; j++ )
          ??????????? {
          ????????????? if( fis.read( buffer )==1024 )
          ??????????????? {
          ????????????????? for( int i=0; i<1024; i++ )
          ???????????????????? {
          ?????????????????????? num++;
          ???????????????????? }
          ??????????????? }
          ??????????? }
          ???????? //*/

          ???????? fis.close();
          ?????? }
          ???? catch( FileNotFoundException fnfe )
          ????????? {
          ??????????? fnfe.printStackTrace();
          ????????? }
          ???? catch( IOException ioe )
          ????????? {
          ??????????? ioe.printStackTrace();
          ????????? }
          ???? catch( Exception e )
          ????????? {
          ??????????? e.printStackTrace();
          ????????? }

          ???? return "";
          ? }


          ? /*****************************************************************************
          ?? *
          ?? * 主函數(shù)入口
          ?? *
          ?? ****************************************************************************/
          ? public static void main( String[] args )
          ? {
          ??? try{ long begin=System.currentTimeMillis();

          ???????? ReadFaster rf=new ReadFaster();

          ???????? System.out.println( "總共耗時(shí):"+( System.currentTimeMillis()-begin )+"毫秒\r\n" );
          ?????? }
          ??? catch( Exception e )
          ???????? {
          ?????????? e.printStackTrace();
          ???????? }
          ? }
          }

          posted on 2007-09-17 01:02 NeedJava 閱讀(875) 評論(1)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 修水县| 治县。| 达尔| 武汉市| 云霄县| 蛟河市| 福清市| 无棣县| 云南省| 兰考县| 武城县| 榆林市| 岗巴县| 托里县| 浦江县| 托克托县| 正镶白旗| 元阳县| 清新县| 厦门市| 武隆县| 孙吴县| 天津市| 沾益县| 故城县| 灵山县| 开远市| 拉孜县| 邢台市| 丰顺县| 巴楚县| 静安区| 明光市| 浮梁县| 沙雅县| 关岭| 昂仁县| 南川市| 莱西市| 洮南市| 弋阳县|