[Java] inputStream byte[]

JTI·2023년 4월 29일
0

☕️  Java

목록 보기
50/59
post-thumbnail
public static void copy() {

	JFileChooser ch = new JFileChooser(".");
	int choice = ch.showOpenDialog(null);
	if(choice == JFileChooser.APPROVE_OPTION) {
		File src = ch.getSelectedFile();
		String filePath = src.getPath();
		int idx = filePath.lastIndexOf(".");
		String ext = filePath.substring(idx); // .부터 끝까지

		filePath = filePath.substring(0, idx) + " copy" + ext; // 복사본

		FileInputStream fis = null;
		FileOutputStream fos = null;

		try {
			fis = new FileInputStream(src); // 원본파일
			fos = new FileOutputStream(filePath);

			int count = -1;
			byte[] buf = new byte[5000];
			while((count = fis.read(buf)) != -1) {
				fos.write(buf, 0, count);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtill.closeAll(fis, fos);
		}
	}
}

write(buf, 0, count)

buf배열안에 시작인덱스([0])부터 갯수만큼 다 읽어라는 뜻.

byte데이터는 어마어마하게 큰 파일이 많다.
그래서 buffer를 이용해서 관리한다.

profile
Fill in my own colorful colors🎨

0개의 댓글