java中io和nio中的行读取速度之小弟我见

java中io和nio中的行读取速度之我见

             NIO这个东西想必大家都比较熟悉,今天我特意查了下NIO和IO的区别以及代码测试了下性能,NIO在速度上的确不占优势,以下:我是用BufferedReader 和FileWriter来进行数据的copy,利用行读取来做的, 

public  void largeFileIO(String inputFile, String outputFile,int count) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(inputFile)));
           
            FileInputStream o = new FileInputStream(inputFile);
//   GZIPInputStream gzipout = new GZIPInputStream(o);//压缩文件
            BufferedReader in = new BufferedReader(new InputStreamReader(o, "utf-8"),1024*10);//10M缓存

            FileWriter fw = new FileWriter(outputFile,true);
            int c=0;
            while (in.ready()) {
                String line = in.readLine();
                System.out.println(c);
                c++;
                fw.append(line + "\n");
                if(c==count){
                 fw.flush();
                 c=0;
                }
            }
            in.close();
            fw.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
 }

不到一分钟拷贝代码已经有400M,速度可见之快,我利用NIO的代码来读取数据进行拷贝传输

 public void readFile(String saveFile,String anlysisFile){
  
      int bufsize=1024*1024*10;
  try {
   /**
    * 輸入輸出流
    */
   File fin=new File(anlysisFile);
   File fout=new File(saveFile);
   /***
       * 輸入輸出管道
       */
      FileChannel fcin =new RandomAccessFile(fin, "r").getChannel();
      ByteBuffer rbuf=ByteBuffer.allocateDirect(bufsize);
            /**
             * 输出管道
             */
      FileChannel fcout=new RandomAccessFile(fout, "rws").getChannel();
      ByteBuffer wbuf=ByteBuffer.allocateDirect(bufsize);
      readFileByLine(bufsize,fcin,fcout,rbuf,wbuf);
     
      System.out.println("OK");
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 
 public void readFileByLine(int bufsize,FileChannel fcin,FileChannel fcout,ByteBuffer rbuf,ByteBuffer wbuf){
  /***
   * 以转行符分割 結尾
   */
  String endterStr = "\n";
  try {
   byte[] bs=new byte[bufsize];
   StringBuffer strbuf=new StringBuffer("");
   /**
    * 利用FileChannel读取buffer中的数据
    */
   while(fcin.read(rbuf)!=-1){
    /**
     * 记录下一个读取的位置
     */
    int rsize=rbuf.position();
    rbuf.rewind();
    rbuf.get(bs);
    rbuf.clear();
    String tempString=new String(bs,0,rsize);
    System.out.println(tempString);
    
    int fromIndex=0;
    int endIndex=0;
    
    
    while((endIndex=tempString.indexOf(endterStr,fromIndex))!=-1){
     String line=tempString.substring(fromIndex,endIndex);
     line=new String(strbuf.toString()+line);
     writeFileByLine(fcout,wbuf,line);
     strbuf.delete(0, strbuf.length());
     fromIndex=endIndex+1;
    }
    
    
    if(rsize>tempString.length()){
     strbuf.append(tempString.substring(fromIndex,tempString.length()));
    }else{
     strbuf.append(tempString.subSequence(fromIndex, rsize));
    }
    
   }
  } catch (IOException e) {
           e.printStackTrace();
  }
  
 };
 
 
 
  public static void writeFileByLine(FileChannel fcout, ByteBuffer wBuffer, String line){
  
         try {
             fcout.write(wBuffer.wrap(line.getBytes()), fcout.size());
         } catch (IOException e) {
 
             e.printStackTrace();
 
         }
 
     }

以上代码借鉴http://www.blogjava.net/jjshcc/archive/2013/12/17/407694.html ,可能是按照字节码读取的吧,然后加了下别的处理,但是单独测试读取的速度也很慢,测试后结论,通过第一种方法来处理数据要比第二种方法处理数据,至少相差100多倍的效率!可能我知道的太少了吧,求大家喷喷我!让我长点知识!