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();
}

}


}

    推荐阅读