package io.github.kuyer.jbase.io;

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

public class NioFileCopy {

	public static void main(String[] args) throws Exception {
		copy("D:\\Work\\hosts\\mirrors\\CentOS-6.7-i386-minimal.iso", "D:\\Work\\hosts\\mirrors\\CentOS6.iso");
	}
	
	// NIO 复制文件
	public static void copy(String inPath, String outPath) throws Exception {
		FileInputStream fis = new FileInputStream(inPath);
		FileOutputStream fos = new FileOutputStream(outPath);
		
		FileChannel fcin = fis.getChannel();
		FileChannel fcout = fos.getChannel();
		
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		int byteRead = fcin.read(buffer);
		while(byteRead != -1) {
			buffer.flip();
			fcout.write(buffer);
			buffer.clear();
			byteRead = fcin.read(buffer);
		}
		
		fcout.close();
		fcin.close();
		
		fos.close();
		fis.close();
	}

}

@@ -85,32 +85,65 @@
 leOutputStream;%0A
+import java.io.RandomAccessFile;%0A
 import java.nio.
@@ -154,16 +154,16 @@
 Buffer;%0A
-
 import j
@@ -282,16 +282,18 @@
 ion %7B%0A%09%09
+//
 copy(%22D:
@@ -388,16 +388,37 @@
 .iso%22);%0A
+%09%09write(%22for test%22);%0A
 %09%7D%0A%09%0A%09//
@@ -959,16 +959,16 @@
 lose();%0A
-
 %09%09fis.cl
@@ -976,12 +976,344 @@
 se();%0A%09%7D
+%0A%09%0A%09public static void write(String str) throws Exception %7B%0A%09%09RandomAccessFile raf = new RandomAccessFile(%22d:/log.txt%22, %22rw%22);%0A%09%09FileChannel channel = raf.getChannel();%0A%09%09%0A%09%09ByteBuffer buffer = ByteBuffer.allocate(1024);%0A%09%09buffer.put(str.getBytes());%0A%09%09buffer.flip();%0A%09%09channel.write(buffer);%0A%09%09%0A%09%09channel.close();%0A%09%09raf.close();%0A%09%7D
 %0A%0A%7D%0A

modify nio file