java.nio将一个文件的内容写入到另一个的文件简单例子
/**
* 将数据从一个通道复制到另一个通道或从一个文件复制到另一个文件
* @author Administrator
*
*/
public class ChannelDemo {
public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream("E://PAGE.txt");
ReadableByteChannel source = in.getChannel();
FileOutputStream out = new FileOutputStream("E://User.txt");
WritableByteChannel destination = out.getChannel();
copyData(source,destination);
source.close();
destination.close();
System.out.println("success");
}
private static void copyData(ReadableByteChannel source,
WritableByteChannel destination) throws IOException {
ByteBuffer buffer = ByteBuffer.allocateDirect(20*1024);
while(source.read(buffer) != -1){
buffer.flip();
while(buffer.hasRemaining()){//剩余可用长度
destination.write(buffer);
}
buffer.clear();
}
}
}
推荐阅读
- 一个人的旅行,三亚
- 一个小故事,我的思考。
- 一个人的碎碎念
- 七年之痒之后
- 我从来不做坏事
- 异地恋中,逐渐适应一个人到底意味着什么()
- 即将到手三百万
- 迷失的世界(二十七)
- live|live to inspire 一个普通上班族的流水账0723
- 遗憾是生活的常态,但孝顺这件事,我希望每一个人都不留遗憾