1-10. File을 이해하기 위한 예제(10)
: 지정된 파일을 지정된 크기(KB)만큼 자르는 예제이다.
package File;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Exercise010 {
public static void main(String[] args) {
String[] input = new String[2];
Scanner sc = new Scanner(System.in);
input[0] = sc.nextLine();
input[1] = sc.nextLine();
if (input.length < 2) {
System.out.println("USAGE : java FileSplit filename SIZE_KB");
System.exit(0); // 프로그램을 종료한다.
}
final int VOLUME = Integer.parseInt(input[1]) * 1000;
try {
String filename = input[0];
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = null;
BufferedOutputStream bos = null;
int data = 0;
int i = 0;
int number = 0;
while((data = bis.read()) != -1) {
if (i%VOLUME==0) {
if (i!=0) {
bos.close();
}
fos = new FileOutputStream(filename + "_."+ ++number);
bos = new BufferedOutputStream(fos);
}
bos.write(data);
i++;
}
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
} // end of try-catch
}
}
'자바 > 입출력 IO' 카테고리의 다른 글
직렬화가 가능한 클래스 만들기 - Serializable, transient (1) (0) | 2022.04.29 |
---|---|
직렬화(Serializtion): Object 입출력 스트림 (0) | 2022.04.29 |
표준 입출력과 File: File(3) (0) | 2022.04.28 |
표준 입출력과 File: File(2) (0) | 2022.04.28 |
표준 입출력과 File: File(1) (0) | 2022.04.27 |