Java IO流读取大数据文件

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
 *
 * 用NIO把20g的文件分割开 生成到temp文件里
 * 然后再用传统的方法去读取每一个小文件
 */
public class ReadLargeTextWithNIO
{
 public static void main(String args[]) throws IOException
 {
  FileInputStream fin = new FileInputStream("C:\\TDDOWNLOAD\\query.log.td");
  FileChannel fcin = fin.getChannel();
  ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 50);
  while(true)
  {
   buffer.clear();
   int flag = fcin.read(buffer);
   if(flag == -1)
   {
    break;
   }
   buffer.flip();
   FileOutputStream fout = new FileOutputStream("d:\\temp\\" + Math.random() + ".log");
   FileChannel fcout = fout.getChannel();
   fcout.write(buffer);
   System.out.println(buffer);
  }
 }
}

你可能感兴趣的:(Java)