Java/Excercise

javaio ch02 FileInputStreamTest4

낭구리 2021. 9. 13. 17:53
package ch02;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInputStreamTest4 {

	public static void main(String[] args) {

		try(FileInputStream fis = new FileInputStream("assets/input2.txt")){
			
			byte[] bs = new byte[10];
			//배열이 10개라 10/10/6이지만 10개를 채워서 다읽게된다
			//ABCDEFGHIJKLMNOPQRSTUVWXYZ(QRST)
			int i;
			
			while ((i = fis.read(bs))!=-1) {
				//나온 숫자만큼만 읽어라.
//				for (byte b : bs) {
//					System.out.print((char)b);
//				}
				for (int j = 0; j < i; j++) {
					System.out.print((char)bs[j]);
				}
				System.out.println(" : " + i + " 바이트 읽음");
			}
			
			
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

'Java > Excercise' 카테고리의 다른 글

javaio ch03 FileOutputStreamTest1  (0) 2021.09.13
javaio ch02 FileInputStreamTest5  (0) 2021.09.13
javaio ch02 FileInputStreamTest3  (0) 2021.09.13
javaio ch02 FileInputStreamTest2  (0) 2021.09.13
javaio ch02 FileInputStreamTest1  (0) 2021.09.13