<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 41,  comments - 40,  trackbacks - 0

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

    另外,最好能放在有上萬張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
    主站蜘蛛池模板: 亚洲精品无码激情AV| 国产成人aaa在线视频免费观看| 国产亚洲精品线观看动态图| 美女18一级毛片免费看| 又黄又爽的视频免费看| 婷婷亚洲综合一区二区| 亚洲第一视频在线观看免费| 免费看又黄又爽又猛的视频软件| 免费在线一级毛片| 一区二区免费在线观看| 亚洲色精品88色婷婷七月丁香| 东北美女野外bbwbbw免费 | 亚洲日本在线免费观看| 亚洲精品资源在线| 一二三四影视在线看片免费| 久久精品国产亚洲av瑜伽| 亚洲成人一区二区| 麻豆精品不卡国产免费看| 亚洲自偷自拍另类图片二区| 最近免费中文字幕大全视频| 另类小说亚洲色图| 亚洲区小说区激情区图片区| 精品熟女少妇a∨免费久久| 亚洲精品成人图区| 永久免费AV无码网站在线观看| free哆拍拍免费永久视频| 亚洲国产综合专区在线电影| 黄色网址免费大全| 男女啪啪免费体验区| 亚洲av无码成h人动漫无遮挡 | 亚洲美女免费视频| 激情小说亚洲色图| 午夜亚洲国产理论秋霞| 四虎永久在线精品免费观看视频| 黄人成a动漫片免费网站| 亚洲AV日韩AV鸥美在线观看| 国产2021精品视频免费播放| 美女黄色免费网站| 亚洲大香人伊一本线| 亚洲美女高清一区二区三区 | 四虎影院在线免费播放|