FileChannel 使用

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


public class ChannelDemo2
{
	
	public static void main(String [] args)
	{
		File fin = new File("F:"+File.separator+"4399.txt");
		File fout = new  File("F:"+File.separator+"5026.txt");
		
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream(fin);
			fos = new FileOutputStream(fout,false);
		

		
		FileChannel fcis = fis.getChannel(); //获取 输入  输出流的通道
		FileChannel fcos = fos.getChannel(); 
		
		ByteBuffer sb = ByteBuffer.allocate(1024);
		
		int len =0;
		while((len = fcis.read(sb))!=-1)
		{
			sb.flip();
			fcos.write(sb);
			sb.clear();
		}
		
		fis.close();
		fos.close();
		fcis.close();
		fcos.close();
		
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch(IOException e)
		{
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(使用,FileChannel)