package io;
import java.io.*;
public class TestIO2 {
public static void main(String[] args) { // 원본 파일 선택 > c:/java.java 엔터치면 // c:/java 사본.java 으로 저장이 되게끔
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// File 객체 만들고 존재하지 않으면 // 그런파일은 없습니다. 라고 출력하고 다시 입력 받으세요. try { String org = null; File f = null;
while (true) { System.out.print("파일 명 > "); org = br.readLine(); System.out.println(org); f = new File(org);
if (!f.exists()) { System.out.println("파일이 존재하지않습니다."); continue; } break; } String name = org.substring(0, org.lastIndexOf('.')); String ext = org.substring(3, org.lastIndexOf('.')); FileInputStream fis = new FileInputStream(f); FileOutputStream fos = new FileOutputStream(name + "사본" + ext); byte[] b = new byte[1024]; int n = 0;
while ((n = fis.read(b)) != -1) { fos.write(b, 0, n); } fis.close(); fos.close(); } catch (IOException e) { e.printStackTrace();
}
}
}

|