时间: 2020-11-26|51次围观|0 条评论

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

}

原文链接:https://blog.csdn.net/w605283073/article/details/8605536

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

本博客所有文章如无特别注明均为原创。
复制或转载请以超链接形式注明转自起风了,原文地址《FileChannel 使用
   

还没有人抢沙发呢~