자바, FileOutputStream 의 write()와 println()의 결과 차이 질문입니다.
조회수 535회
안녕하세요, 자바를 처음부터 공부하고 있는 학생입니다.
지금은 책 보면서 하나하나 따라하고 있는 수준인데요, Stream 을 공부하다가 의문이 생겨서 질문합니다.
import java.io.FileOutputStream;
public class TestCode {
static public void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("test.txt");
byte[] data = "ABC".getBytes();
for(int i = 0; i < data.length; i++){
System.out.println(data[i]);
fos.write(data[i]);
}
fos.flush();
fos.close();
}
}
자바8 도큐먼트에서의 write(int b) 에 대한 설명은 아래와 같습니다.
Writes the specified byte to this file output stream.
위 코드에서 System.out.println(data[i]); 구문은 65, 66, 67 을 각각 출력합니다. 그리고 text.txt 파일에는 ABC 가 출력됩니다.
어째서 test.txt 에는 65, 66, 67 이 아닌 ABC 가 출력된 것인가요? println() 으로 콘솔에 출력하는 것과 write() 로 파일에 출력하는 것의 차이는 무엇인가요?
선배님들의 많은 가르침 바랍니다.
감사합니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력