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);
}
}
}

版权声明:本文为rfv1116原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。