java 怎么读取多个txt文件

如题所述

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);
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-10-27
啊 你会读取一个文件么?会一个得话,不会读取多个?
相似回答