JAVA NIO - Perforemance 比较

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次, 效率如下。

The interval is 133048
The interval is 3249
The interval is 356
The interval is 337

拷贝大约30M文件10次,效率入校

The interval is 1337587
The interval is 33179
The interval is 3531
The interval is 3455

然后看到FileChanel的transferFrom方法觉得效率可能会好一些,然后试了一下

The interval is 176 可以看出FileChanel的该方法是非常高效的。当然transferTo的效率也很高。


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



你可能感兴趣的:(nio)