FileChannel 使用

来源:互联网 发布:学车网络招生平台 编辑:程序博客网 时间:2024/06/07 03:08
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();}}}

原创粉丝点击