【Java IO】NIO复制文件

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!");
	}
}

你可能感兴趣的:(【Java IO】NIO复制文件)