java

[Java] 파일복사

호랭형님 2021. 10. 21. 21:46
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy {
	public static void main(String[] args) {
		String file_path = "원본파일경로";
		String copy_path = "복사된 파일 경로";

		try {
			FileInputStream input = new FileInputStream(new File(file_path));
			FileOutputStream output = new FileOutputStream(new File(copy_path));

			byte[] buf = new byte[1024];
			int readData;
			while((readData = input.read(buf))>0) {
				output.write(buf, 0, readData);
			}
			input.close();
			output.close();


		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

원본문서를 다른 폴더로 복사해야 될 일이 있어서 작성해본 코드입니다.

잘못된 부분이 있거나 더 좋은 코드 있으면 알려주세요~