package com.hp.test.nio;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
*
* 用NIO读取大文本(1G以上)
*
* @author landon
*
*/
public class ReadLargeTextWithNIO
{
public static void main(String[] args) throws IOException
{
FileInputStream fin = new FileInputStream("e:/nio/aaa.log");
FileChannel fcin = fin.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 10);
int i=0;
while(true)
{
i+=1;
buffer.clear();
int flag = fcin.read(buffer);
if(flag == -1)
{
break;
}
buffer.flip();
FileOutputStream fout = new FileOutputStream("e:/nio/s/"+i+".log");
FileChannel fcout = fout.getChannel();
fcout.write(buffer);
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
*
* 用NIO读取大文本(1G以上)
*
* @author landon
*
*/
public class ReadLargeTextWithNIO
{
public static void main(String[] args) throws IOException
{
FileInputStream fin = new FileInputStream("e:/nio/aaa.log");
FileChannel fcin = fin.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 10);
int i=0;
while(true)
{
i+=1;
buffer.clear();
int flag = fcin.read(buffer);
if(flag == -1)
{
break;
}
buffer.flip();
FileOutputStream fout = new FileOutputStream("e:/nio/s/"+i+".log");
FileChannel fcout = fout.getChannel();
fcout.write(buffer);
}
}
}