다연이네

[days25] 파일 복사하기 (1. 실행파일 2.txt파일) 본문

Java

[days25] 파일 복사하기 (1. 실행파일 2.txt파일)

 다연  2020. 10. 25. 14:37
반응형

[실행파일 복사]
FileInputStream, FileOutputStream

 

1. 그냥 복사 - 느리다

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Re04 {

	public static void main(String[] args) {
		String originalpath = "C:\\Users\\82103\\Pictures\\Saved Pictures\\dayon.jpg";
		// 원래 있는 사진경로\\사진명
		String copypath = "C:\\Users\\82103\\Pictures\\Camera Roll\\copydayon.jpg";
		// 복사할 경로\\사진명
		
		fileCopy_byteStream(originalpath, copypath);
		System.out.println("=END=");

	}

	private static void fileCopy_byteStream(String originalpath, String copypath)  {
		//처리시간 확인을 위해 nanoTime() 사용
		long startTime = System.nanoTime();

		File originalfile = new File(originalpath); // 원래 파일
		System.out.println(originalfile.length() +"byte 복사 예정");
		
		InputStream is = null;
		OutputStream os = null;
		
		try {
			is = new FileInputStream(originalfile); //원래 파일을 IN
            					//이 대신에 originalpath 삽입해도 됨
			os = new FileOutputStream(copypath); //복사 경로에 OUT
			
			int b = 0;
			while((b=is.read())!=-1) {
				os.write(b); //os에 is에서 읽은 정보들(b) 넣기
			}
			os.flush();
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				is.close();
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		
		 long estimatedTime = System.nanoTime() - startTime;
		System.out.println("> 처리시간: "+estimatedTime+"ns");
	}

}

출력값

3615290byte 복사 예정 
> 처리시간: 54314257000ns 
=END=

 

2. 보조 스트림 사용하여 복사 - 빠르다

+ BufferedInputStream, BufferedOutputStream

package test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Re04 {

	public static void main(String[] args) {
		String originalpath = "C:\\Users\\82103\\Pictures\\Saved Pictures\\dayon.jpg";
		String copypath = "C:\\Users\\82103\\Pictures\\Camera Roll\\copydayon.jpg";

		
		fileCopy_byteStream(originalpath, copypath);
		System.out.println("=END=");

	}

	private static void fileCopy_byteStream(String originalpath, String copypath)  {

		long startTime = System.nanoTime();

		File originalfile = new File(originalpath); 
		System.out.println(originalfile.length() +"byte 복사 예정");
		
		InputStream is = null;
		OutputStream os = null;
		
		//
		final int N = 1024;
		byte[] buffer = new byte[N];
		BufferedInputStream bis = null;
		BufferedOutputStream bos= null;
		
		try {
			is = new FileInputStream(originalfile); 
			os = new FileOutputStream(copypath); 
			
			//
			bis = new BufferedInputStream(is, N);
			bos = new BufferedOutputStream(os, N);
			
			int n = 0;
			while((n=bis.read(buffer))!=-1) {
				bos.write(buffer, 0, n);
			}
			
			os.flush();
			bos.flush();
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				is.close();
				os.close();
				
				bis.close();
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		
		 long estimatedTime = System.nanoTime() - startTime;
		System.out.println("> 처리시간: "+estimatedTime+"ns");
	}

}

출력값

3615290byte 복사 예정 
> 처리시간: 124216100ns 
=END= 

 

 

[텍스트파일 복사]

FileReader, FileWriter

 

1. 그냥 복사 - 느리다

package test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Re08 {

	public static void main(String[] args) {
		String originalpath = ".\\src\\days25\\Ex04.java";
		String copypath = "C:\\Class\\Ex04_Copy.java";

		
		fileCopy_textStream(originalpath, copypath);
		System.out.println("=END=");

	}

	private static void fileCopy_textStream(String originalpath, String copypath)  {

		long startTime = System.nanoTime();

		File originalfile = new File(originalpath); 
		System.out.println(originalfile.length() +"byte 복사 예정");
		
		FileReader fr = null;
		FileWriter fw = null;
		
		try {
			fr = new FileReader(originalfile); //원래 파일을 IN
			fw = new FileWriter(copypath); //복사 경로에 OUT
			
			int b = 0;
			while((b=fr.read())!=-1) {
				fw.write(b); //os에 is에서 읽은 정보들(b) 넣기
			}
			fw.flush();
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				fr.close();
				fw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		
		 long estimatedTime = System.nanoTime() - startTime;
		System.out.println("> 처리시간: "+estimatedTime+"ns");
	}

}

출력값

1823byte 복사 예정
> 처리시간: 17144200ns
=END=

 

2. 보조 스트림 사용하여 복사 - 빠르다

+ bufferedReader, bufferedWriter

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Re08 {

	public static void main(String[] args) {
		String originalpath = ".\\src\\days25\\Ex04.java";
		String copypath = "C:\\Class\\Ex04_Copy.java";

		
		fileCopy_textStream(originalpath, copypath);
		System.out.println("=END=");

	}

	private static void fileCopy_textStream(String originalpath, String copypath)  {

		long startTime = System.nanoTime();

		File originalfile = new File(originalpath); 
		System.out.println(originalfile.length() +"byte 복사 예정");
		
		FileReader fr = null;
		FileWriter fw = null;
		
		//
		final int N = 1024;
		char[] buffer = new char[N];
		BufferedReader br = null;
		BufferedWriter bw = null;
		
		try {
			fr = new FileReader(originalfile); 
			fw = new FileWriter(copypath); 
			
			br = new BufferedReader(fr, N);
			bw = new BufferedWriter(fw, N);
			
			int n = 0;
			while((n=br.read(buffer))!=-1) {
				bw.write(buffer, 0, n); 
			}
			fw.flush();
			bw.flush();
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				fr.close();
				fw.close();
				
				br.close();
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
		
		 long estimatedTime = System.nanoTime() - startTime;
		System.out.println("> 처리시간: "+estimatedTime+"ns");
	}

}

출력값

1823byte 복사 예정
> 처리시간: 2881500ns
=END=

반응형
Comments