`

nio分割普通文件

阅读更多
package utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ReadLargeTextWithNIO 
{
 public static void main(String...args) throws IOException
 {
  String filename="Visual.zip";
  FileInputStream fin = new FileInputStream("c:\\2011-01-18\\"+filename);
  FileChannel fcin = fin.getChannel();
 
  ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 20);
  
  int n=0;
  while(true)
  {
   buffer.clear();
   
   int flag = fcin.read(buffer);
   
   if(flag == -1)
   {
    break;
   }
   
   buffer.flip();
   n++;
   FileOutputStream fout = new FileOutputStream("c:\\2011-01-18\\"+filename+"."+n + ".bak");
   FileChannel fcout = fout.getChannel();
   
   fcout.write(buffer);
  }
 }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics