본문 바로가기
JAVA SE/Code

CODE [20.07.30/Day_16] Java SE / 파일 복사 붙여넣기 프로그램을 만들어보자 !

by 파프리카_ 2020. 7. 30.
728x90
반응형

파일 경로만 지정해주면, 파일을 복사해서 붙여넣는 application을 구현해보자


1. 텍스트 파일 버전

 

main class

/TestCopyAndPasteService.java

package step7;

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

public class TestCopyAndPasteService {
	public static void main(String[] args) {
		String copyPath =
				"C:" + File.separator + 
				"kosta203" + File.separator + 
				"iotest3"+ File.separator + "test.txt";
		
		String pastePath = 
				"C:" + File.separator + 
				"kosta203" + File.separator + 
				"iotest4"+ File.separator + "test_copied.txt";
		
		CopyAndPasteService service = new CopyAndPasteService();
		//try
		try {
			service.execute(copyPath, pastePath);
			System.out.println("복사 붙여넣기 완료!");
		}

		//catch
		catch(FileNotFoundException fe)
		{
			fe.printStackTrace();
		}
		
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}

 

Service class

/CopyAndPasteService.java

package step7;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class CopyAndPasteService {
	
	public void execute(String copyPath, String pastePath) throws IOException {
		
		//1. pasteCopy를 이용해 디렉토리 생성.
		File file = new File(pastePath); //pastePath 파일의 path
		File dir = file.getParentFile(); //pastePath파일이 들어갈 directory의 path
		dir.mkdirs(); //iotest4 디렉토리 생성
		
		//2. copyPath를 통해 파일 객체 가져오기(읽기 reader)
		FileReader fr = new FileReader(copyPath);
		BufferedReader br = new BufferedReader(fr);
		
		//3. 읽을 정보를 담아 pastePath에 넣어줄 list 선언
		ArrayList<String> list = new ArrayList<String>();
		
		//4. 정보 읽어서 list에 담기
		while (br.ready()) {
			list.add(br.readLine());
		}
		
		//5. pastePath에 파일객체 만들기(쓰기 writer)
		FileWriter fw = new FileWriter(pastePath);
		PrintWriter pw = new PrintWriter(fw);
		
		//6. 정보 입력(쓰기)
		for (int i = 0; i < list.size(); i++) {
			pw.println(list.get(i));
		}
		
		//7. 파일 닫기
		pw.close();
		br.close();
	}
}

 

짧은 버전 + 예외발생 시에도 stream을 close()해줄 수 있는 코드

/CopyAndPasteService2.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class CopyAndPasteService2 {

	public void execute(String copyPath, String pastePath) throws IOException {

		// 1. pasteCopy를 이용해 디렉토리 생성.
		new File(pastePath).getParentFile().mkdirs();
		BufferedReader br = null;
		PrintWriter pw = null;
		
		try 
		{
			// 2. copyPath를 통해 파일 객체 가져오기(읽기 reader)
			br = new BufferedReader(new FileReader(copyPath));

			// 3. pastePath에 파일객체 만들기(쓰기 writer)
			pw = new PrintWriter(new FileWriter(pastePath));

			// 4. 정보 읽어서 정보 입력(쓰기)
			while (br.ready()) {
				pw.println(br.readLine());
			}
		}

		finally //예외 발생 시에도 반드시 stream을 닫아준다.
		{	// 5. 파일 닫기
			if(br != null)
				br.close();
			if(pw != null)
				pw.close();
		}
	}
}

 


결과

 

 


2. 이미지 파일 버전

 

main class

/TestImageService.java

package step8;

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

public class TestImageService {
	public static void main(String[] args) {
		String copyPath =
				"C:" + File.separator + 
				"kosta203" + File.separator + 
				"0myung.png";
		
		String pastePath = 
				"C:" + File.separator + 
				"kosta203" + File.separator + 
				"iotest4"+ File.separator + "0myung_copied.png";
		
		ImageService service = new ImageService();
		try 
		{
			service.copyAndPasteImage(copyPath, pastePath);
			System.out.println("이미지 복사 붙여넣기 완료!");
		} 
		
		catch (FileNotFoundException fe) {
			fe.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}

 

Service class

/ImageService.java

package step8;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageService {
	/**
	 * 이미지 처리를 위해 byte(8bit) 스트림을 이용한다
	 * 입력스트림 : 노드스트림 - FileInputStream, 프로세스 스트림 - BufferedInputStream
	 * 출력스트림 : 노트스트림 - FileOuputStream, 프로세스 스트림 - BufferedOutputStream
	 * @param copyPath
	 * @param pathPath
	 * @throws IOException
	 */
	public void copyAndPasteImage(String copyPath, String pastePath) throws IOException {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		
		try
		{
			bis = new BufferedInputStream(new FileInputStream(copyPath));
			bos = new BufferedOutputStream(new FileOutputStream(pastePath));
			
			// 데이터 읽기
			int data = bis.read();
			while (data != -1) {
				bos.write(data);
				data = bis.read();
			}
		} 
		
		finally
		{
			if (bis != null)
				bis.close();
			if (bos != null)
				bos.close();
		}
	}
}

 


결과

728x90
반응형