Byte 단위 입출력
Byte단위 입출력 클래스는 클래스의 이름이 InputStream이나 OutputStream으로 끝납니다.
- 파일로 부터 1byte씩 읽어들여 파일에 1byte씩 저장하는 프로그램을 작성
- 파일로 부터 읽어오기 위한 객체 - FileInputStream
- 파일에 쓸수 있게 해주는 객체 - FileOutputStream .
- read()메소드가
- byte를 리턴한다면 끝을 나타내는 값을 표현할 수가 없기 때문에, byte가 아닌 int를 리턴한다.
- 음수의 경우 맨 좌측 비트가 1이 된다. 읽어들일 것이 있다면 항상 양수를 리턴한다고볼 수 있다. .
- FileInputStream과 FileOutputStream을 이용하여, 1바이트씩 읽어들여 1바이트씩 저장
- read()메소드가 리턴하는 타입은 정수인데, 정수 4바이트중 마지막 바이트에 읽어들인 1바이트를 저장한다.
- read()메소드는 더이상 읽어들일 것이 없을때 -1을 리턴한다.
public class ByteIOExam1 {
public static void main(String[] args){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("src/javaIO/exam/ByteExam1.java");
fos = new FileOutputStream("byte.txt");
int readData = -1;
while((readData = fis.read())!= -1){
fos.write(readData);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
-
byte는 음수표현을 할 수 없나요?
sang0436
2021.1.13 15:59
0
-
write 메소드 질문입니다.
프로그래머
2020.7.8 01:54
0
-
Write에 질문드립니다.
YOONTAEJUN
2019.11.12 15:13
0
-
변수 int readData에 -1을 저장한이유가 궁금합니다 아무것도 저장안하고 in...
오형남
2019.5.25 18:30
1
-
read()
이현수
2018.5.2 01:17
0
-
닫아준다!
-
2018.2.2 15:23
1
-
byte대신에 int를 사용하는 이유에 대해서 질문있습니다.
-
2017.5.21 00:44
1
-
질문있습니다.
권용호
2016.7.24 15:04
1
-
finallly에서 input/output 각각 try/catch 한 이유?
-
2016.6.20 08:33
1
-
FileInputStream과 FileOutputStream은 static 클래스인가요?
Park Hansu
2016.6.18 15:28
1