상세 컨텐츠

본문 제목

FileInputStream 클래스 read() 예제

Java

by kwanghyup 2020. 6. 14. 18:31

본문

FileOutputStreamTest 클래스파일 위치와 같은 경로 test.txt

한글한글ABCD

 

FileOutputStreamTest 클래스 

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        String path = FileInputStreamTest.class.getResource("test.txt").getPath();
        InputStream inputStream = new FileInputStream(path);

        int i = 0;  // 반복수 저장
        int readed; // 읽은 바이트 저장
        byte[] bytes = new byte[3]; // 바이트 배열
        StringBuffer sb = new StringBuffer(); // 스트링버퍼에 읽은 값 누적
        while ((readed=inputStream.read(bytes))!=-1){
            String str = new String(bytes); // 한번 반복시 읽는 바이트값 인스턴스 저장
            sb.append(str); // 스트링버퍼에 누적
            System.out.println("읽은 값: " + str);
            System.out.println("읽은 바이트 수 : " + readed);
            i++; // 반복수 증가
        }
        inputStream.close();

        System.out.println("총 읽은 문자 : " + sb);
        System.out.println("반복 수 : [" + i +"]");

    }
}

 

'Java' 카테고리의 다른 글

System.out 예제  (0) 2020.06.14
Systme.in 예제  (0) 2020.06.14
FileOutStream write()메소드 예제  (0) 2020.06.14
FileWriter 클래스 write() 메소드 예제  (0) 2020.06.14
FileRead 클래스 read(char cbuf, offset, length) 예제  (0) 2020.06.14

관련글 더보기

댓글 영역