您的位置:首页 > 其它

利用filechannel拷贝文件内容

2014-10-15 19:36 302 查看
package com.itbuluoge.nio;

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

public class GetChannel {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {

/*文件内容拷贝*/
FileChannel fca=new FileInputStream("data.txt").getChannel();
FileChannel fcb=new FileOutputStream("back.txt").getChannel();

ByteBuffer swap=ByteBuffer.allocate(1000);
fca.read(swap);
while(swap.hasRemaining())
{
swap.flip();
/*写入到新文件*/
fcb.write(swap);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: