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