import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class ex6 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
FileInputStream f1 = null;
FileInputStream f2 = null;
FileOutputStream fo = null;
System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우, vk일은 프로젝트 폴더에 있어야 합니다.");
System.out.print("첫번째 파일 이름을 입력하세요>>");
String fileName1 = sc.next();
System.out.print("두번째 파일 이름을 입력하세요>>");
String fileName2 = sc.next();
try {
f1 = new FileInputStream("C:\\temp\\"+fileName1);
f2 = new FileInputStream("C:\\temp\\"+fileName2);
fo = new FileOutputStream("C:\\temp\\appended.txt");
byte[] f1Buf = new byte[1024];
byte[] f2Buf = new byte[1024];
int f1N=0,f2N;
while(true) {
f1N = f1.read(f1Buf);
f2N = f2.read(f2Buf);
fo.write(f1Buf,0,f1N);
fo.write(f2Buf,0,f2N);
if(f1N<f1Buf.length&&f2N<f2Buf.length) {
break;
}
}
System.out.println("프로젝트 폴더 밑에 appended.txt 파일에 저장하였습니다.");
if(f1 != null) f1.close();
if(f2 != null) f2.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
결과
전체 경로명이 아닌 파일 이름만 입력하는 경우, vk일은 프로젝트 폴더에 있어야 합니다.
첫번째 파일 이름을 입력하세요>>elvis1.txt
두번째 파일 이름을 입력하세요>>elvis2.txt
프로젝트 폴더 밑에 appended.txt 파일에 저장하였습니다.
'명품JAVA프로그래밍 > 8장 입출력 스트림과 파일 입출력' 카테고리의 다른 글
[명품JAVA프로그래밍] 8장 실습문제 9번 (0) | 2022.02.25 |
---|---|
[명품JAVA프로그래밍] 8장 실습문제 8번 (0) | 2022.02.24 |
[명품JAVA프로그래밍] 8장 실습문제 5번 (0) | 2022.02.24 |
[명품JAVA프로그래밍] 8장 실습문제 4번 (0) | 2022.02.24 |
[명품JAVA프로그래밍] 8장 실습문제 3번 (0) | 2022.02.24 |