您的位置:首页 > 编程语言 > Java开发

【Java IO】NIO复制文件

2015-01-11 15:56 567 查看
package com.zzj.nio;

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

public class FileChannelTest {
public static void main(String[] args) throws IOException {
String name = FileChannelTest.class.getResource("/video.avi").getPath();
FileInputStream inStream = new FileInputStream(name);
FileOutputStream outStream = new FileOutputStream(name + ".bak");
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (inChannel.read(byteBuffer) > -1) {
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
inChannel.close();
outChannel.close();
System.out.println("end!");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: