java 怎么读取多个txt文件
2个回答
展开全部
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);
}
}
}
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);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询