NIO 复制文件。

package com.zf.nio;

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

public class NioTest1 {
	
	public static void main(String[] args) throws Exception{
		
		FileInputStream fis = new FileInputStream("C:/Documents and Settings/Administrator/桌面/XML高级编程.rar");
		FileOutputStream fos = new FileOutputStream("D:/XML高级编程.rar");
		
		FileChannel in = fis.getChannel();
		FileChannel out = fos.getChannel();
		
		ByteBuffer bb = ByteBuffer.allocate(1024); 
		int len = -1 ;
		while(true){	
			bb.clear();	//读之前先清空
			len =in.read(bb);
			if(len == -1)
				break ;
			bb.flip();	//写之前设置position 
			out.write(bb);
		}
		
	}

}

你可能感兴趣的:(NIO 复制文件。)