package nio; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class FileNIOTester { public static void main(String args[]) { try { long start = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { ioStreamCopy(); } long end = System.currentTimeMillis(); System.out.println("The interval is " + (end - start)); long start1 = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { //ioStreamCopy(); charsCopy(); } long end1 = System.currentTimeMillis(); System.out.println("The interval is " + (end1 - start1)); long start2 = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { //ioStreamCopy(); lineCopy(); } long end2 = System.currentTimeMillis(); System.out.println("The interval is " + (end2 - start2)); long start3 = System.currentTimeMillis(); for (int i = 0; i < 10; i++) { //ioStreamCopy(); lineCopy(); } long end3 = System.currentTimeMillis(); System.out.println("The interval is " + (end3 - start3)); } catch (Exception e) { e.printStackTrace(); } } public static void ioStreamCopy() throws Exception { InputStream in = new FileInputStream("helloWorld.txt"); byte[] bs = new byte[4096]; int b = -1; int start = 0; OutputStream out = new FileOutputStream("copyhelloworld.txt"); while ((b = in.read()) != -1) { bs[start] = (byte) b; start++; if (start == 4096) { out.write(bs, 0, start); start = 0; } } out.write(bs, 0, start); // System.out.println(new String(bs, 0, start)); in.close(); out.close(); } public static void charsCopy() throws Exception { // TODO Auto-generated method stub try { Reader reader = new FileReader("helloworld.txt"); Writer writer = new FileWriter("copyhelloworld.txt"); int c = -1; while((c= reader.read())!= -1){ writer.write(c); } writer.flush(); writer.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); } } public static void lineCopy() throws Exception { try { BufferedReader reader = new BufferedReader(new FileReader("helloworld.txt")); BufferedWriter writer = new BufferedWriter(new FileWriter("copyhelloworld.txt")); String str = ""; while((str = reader.readLine())!= null){ writer.write(str); } writer.flush(); writer.close(); reader.close(); } catch (Exception e) { e.printStackTrace(); } } public static void nioCopy() throws Exception{ FileInputStream in = new FileInputStream("helloWorld.txt"); FileOutputStream out = new FileOutputStream("copyhelloworld.txt"); FileChannel fc = in.getChannel(); FileChannel fo = out.getChannel(); ByteBuffer bb = ByteBuffer.allocate(4096); while(true){ bb.clear(); int r = fc.read(bb); if (r == -1) { break; } bb.flip(); fo.write(bb); } in.close(); fo.close(); } }
拷貝大約2M文件10次, 效率以下。 java
The interval is 133048
The interval is 3249
The interval is 356
The interval is 337 spa
The interval is 1337587
The interval is 33179
The interval is 3531
The interval is 3455 code
而後看到FileChanel的transferFrom方法以爲效率可能會好一些,而後試了一下 ip
The interval is 176 能夠看出FileChanel的該方法是很是高效的。固然transferTo的效率也很高。 get
public static void nioTransferFromCopy() throws Exception { FileInputStream in = new FileInputStream("helloWorld.txt"); FileOutputStream out = new FileOutputStream("copyhelloworld.txt"); FileChannel fc = in.getChannel(); FileChannel fo = out.getChannel(); fo.transferFrom(fc, 0, fc.size()); fc.close(); fo.close(); } public static void nioTransferToCopy() throws Exception { FileInputStream in = new FileInputStream("helloWorld.txt"); FileOutputStream out = new FileOutputStream("copyhelloworld.txt"); FileChannel fc = in.getChannel(); FileChannel fo = out.getChannel(); fc.transferTo(0, fc.size(), fo); fc.close(); fo.close(); }