〔Java〕看看谁读的快

〔Java〕看看谁读的快

刚刚写的看谁复制的快,只是由于在项目中犹豫到底是用哪个好而写的,没想到大家很感兴趣,那我再把读取文件谁快也翻上来,有错尽管拍砖。

另外,最好能放在有上万张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();
         }
  }
}

你可能感兴趣的:(〔Java〕看看谁读的快)