✏️ File 복사하기
- 원본 파일 표시
- 복사본 파일 표시
- 복사하기
import javax.swing.*;
import java.io.*;
public class FileCopy {
public static JFileChooser chooser = new JFileChooser();
public static File oriFile;
public static File copyFile;
public static void origInfo() {
int choice = chooser.showOpenDialog(null);
if(choice == JFileChooser.APPROVE_OPTION) {
oriFile = chooser.getSelectedFile();
System.out.println(oriFile.getPath());
}
}
public static void copyInfo() {
int choice = chooser.showSaveDialog(null);
if(choice == JFileChooser.APPROVE_OPTION) {
copyFile = chooser.getSelectedFile();
System.out.println(copyFile.getPath());
}
}
public static void copy() {
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
fis = new FileInputStream(oriFile);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
pw = new PrintWriter(new FileOutputStream(copyFile));
String line = "";
while( (line = br.readLine()) != null ) {
pw.write(line);
}
pw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtil.closeAll(br, pw);
}
}
public static void main(String[] args) {
origInfo();
copyInfo();
copy();
}
}