剛剛寫的看誰復制的快,只是由于在項目中猶豫到底是用哪個好而寫的,沒想到大家很感興趣,那我再把讀取文件誰快也翻上來,有錯盡管拍磚。
另外,最好能放在有上萬張10KB以上的圖片的文件夾下運行,否則不一定看出效果,我的是六千多張,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
{
? /*****************************************************************************
?? *
?? * 構造函數,默認使用當前路徑
?? *
?? ****************************************************************************/
? public ReadFaster()
? {
??? this( "." );
? }
? public ReadFaster( String fileName )
? {
??? this.listPictures( null, fileName );
? }
? /*****************************************************************************
?? *
?? * 列出當前目錄下的文件列表,包括文件和文件夾
?? *
?? ****************************************************************************/
? private final void listPictures( File path, String fileName )
? {
??? File file=new File( path, fileName );
??? if( file.isDirectory() )
????? {
??????? //得到當前目錄下的文件列表,包括文件和文件夾
??????? 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 );
?????????????? }
?????????? }
? }
? /*****************************************************************************
?? *
?? * 根據后綴名判斷是否是有效的圖片,并且返回小寫的后綴名
?? *
?? ****************************************************************************/
? 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的默認有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 "";
? }
? /*****************************************************************************
?? *
?? * 主函數入口
?? *
?? ****************************************************************************/
? public static void main( String[] args )
? {
??? try{ long begin=System.currentTimeMillis();
???????? ReadFaster rf=new ReadFaster();
???????? System.out.println( "總共耗時:"+( System.currentTimeMillis()-begin )+"毫秒\r\n" );
?????? }
??? catch( Exception e )
???????? {
?????????? e.printStackTrace();
???????? }
? }
}
posted on 2007-09-17 01:02
NeedJava 閱讀(873)
評論(1) 編輯 收藏 所屬分類:
Java