자바/입출력 IO
표준 입출력과 File: File(4)
백_곰
2022. 4. 28. 17:32
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
}
}