반응형
package FileCopy;
import java.io.*;
public class Main {
	public static void main(String[] args) {
		final int SIZE = 100;
		InputStream is = null;
		OutputStream os = null;
		int cnt;
		byte[] b = new byte[SIZE];
		try {
			is = new FileInputStream("test.txt"); // 원본
			os = new FileOutputStream("test2.txt"); // 복사본
			while (true) {
				cnt = is.read(b);
				if (cnt < SIZE) {
					os.write(b, 0, cnt);
					break;
				}
				os.write(b);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				if (is != null)
					is.close();
				if (os != null)
					os.close();
				System.out.println("파일 복사 성공!");
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
	}
}

 

반응형

+ Recent posts