JAVA NIO - Perforemance 비교

4748 단어 nio
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 의 transfer From 방법 을 보고 효율 성 이 좋 을 것 같 아서 해 봤 어 요.
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();
	}

좋은 웹페이지 즐겨찾기