시나리오 코드 1 - 문자기반 스트림을 활용한 파일복사 클래스 설계하기
package Io.file.ch07;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class FileCopyHelper {
// 클래스를 함수기반으로 나누는 연습을 많이 하자
// 파일 복사
public static void copyFile(String readFilePth, String WriterFilePath) {
try (FileReader fr = new FileReader(readFilePth);
FileWriter fw = new FileWriter(WriterFilePath)) {
int c;
while ((c = fr.read()) != -1) {
fw.write(c);
}
System.out.println("파일 복사 완료 : " + WriterFilePath);
} catch (Exception e) {
e.printStackTrace();
System.out.println("파일 복사 중 오류 발생");
}
}
// 파일 복사 - 버퍼 활용
public static void copyFileWithBuffer(String readFilePath, String writerFilePath) {
try ( // 버퍼 주 스트림은 바로 접근이 안되어 생성해줘야한다.
BufferedReader bufferedReader = new BufferedReader(new FileReader(readFilePath));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(writerFilePath))){
// 버퍼를 활용하는 버퍼의 크기를 지정할 수 있다.
char[] buffer = new char[1024];
int numCharsRead; // 읽은 문자 수
while((numCharsRead = bufferedReader.read(buffer)) != -1) {
// 0번째 부터 numCharsRead 까지 써라
bufferedWriter.write(buffer, 0, numCharsRead);
System.out.println("numCharsRead : " + numCharsRead);
}
System.out.println("버퍼를 사용한 파일 복사 완료 : " + writerFilePath);
} catch (Exception e) {
e.printStackTrace();
System.out.println("버퍼를 사용한 파일 복사 중 오류 발생");
}
}
// 메인 함수
public static void main(String[] args) {
FileCopyHelper.copyFile("Seoul.txt", "copySeoul.txt");
System.out.println("]------'------'----[");
FileCopyHelper.copyFileWithBuffer("NewYork.txt", "copyNewYork.txt");
} // end of main
} // end of class
실행
파일 복사 완료 : copySeoul.txt
]------'------'----[
numCharsRead : 257
버퍼를 사용한 파일 복사 완료 : copyNewYork.txt
시나리오 코드 2 - 바이트 기반 스트림을 활용한 Zip 파일 만들어 보기
package Io.file.ch07;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipFileHelper {
public static void zipFile(String fileToZip, String zipFileName) {
// ZipOutputStream을 사용해서 ZIP 형식으로 데이터를 압출할 수 있다.
// FileOutputStream을 활용해서 설정
try (FileInputStream fis = new FileInputStream(fileToZip);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName))) {
// ZipEntry 객체 생성 - 압축 파일 내에서 개별 파일을 나타냅니다.
ZipEntry zipEntry = new ZipEntry(fileToZip);
zos.putNextEntry(zipEntry);
// 파일 내용을 읽고 ZIP 파일에 쓰기 위한 버퍼 생성
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
System.out.println("ZIP 파일 생성완료 : " + zipFileName);
} catch (Exception e) {
e.printStackTrace();
System.out.println("ZIP 파일 생성시 오류 발생 : " + zipFileName);
}
}
// 파일을 압축하는 기능 - zip
public static void main(String[] args) {
ZipFileHelper.zipFile("Seoul.txt", "zipSeoul.zip");
} // end of main
} // end of class
실행
ZIP 파일 생성완료 : zipSeoul.zip
'Java > JAVA 3 - 유용한 클래스' 카테고리의 다른 글
사용자모드와 커널모드란? - 18 (0) | 2024.05.22 |
---|---|
로그와 파일 저장 - 17 (0) | 2024.05.21 |
파일 출력 스트림(문자 기반 스트림) - 15 (0) | 2024.05.21 |
파일 입력 스트림(문자 기반 스트림) - 14 (0) | 2024.05.21 |
문자 기반 스트림 - 13 (0) | 2024.05.21 |