Byte 단위 입출력 심화
Byte단위 입출력 클래스는 클래스의 이름이 InputStream이나 OutputStream으로 끝납니다.
- 파일로 부터 512byte씩 읽어들여 파일에 512byte씩 저장하는 프로그램을 작성
- byte[] buffer = new byte[512];
- 512byte만큼 읽어 들이기 위해 byte배열을 사용
public class ByteIOExam1 {
public static void main(String[] args){
//메소드가 시작된 시간을 구하기 위함
long startTime = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("src/javaIO/exam/ByteExam1.java");
fos = new FileOutputStream("byte.txt");
int readCount = -1;
byte[] buffer = new byte[512];
while((readCount = fis.read(buffer))!= -1){
fos.write(buffer,0,readCount);
}
} 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();
}
}
//메소드가 끝났을때 시간을 구하기 위함.
long endTime = System.currentTimeMillis();
//메소드를 수행하는데 걸린 시간을 구할 수 있음.
System.out.println(endTime-startTime);
}
}
- System.currentTimeMillis();
- 현재 시간을 롱타입으로 반환한다.
-
저번 강의와 혼동이 오네요
sang0436
2021.1.14 15:24
0
-
전 강의에서는 파일이 잘 만들어졌는데
ㅇㅇ
2020.12.17 22:31
0
-
저번 강의부터 안 되는 건데
나윤하
2020.10.26 11:16
0
-
fis.read(buffer) 에 관련하여 질문 드립니다.
-
2019.1.11 02:28
1
-
운영체제에서 512바이트씩 읽어들인다는 것이..
-
2017.4.26 08:01
1
-
복습하고 있는데, 이번 강의에서 또 막히네요ㅜㅜ
Park Hansu
2016.6.28 21:04
2
-
int readCount에 512byte를 저장하고 있는 건가요?
Park Hansu
2016.6.18 16:13
1