2011-05-04 2 views
0

데이터를 읽는 중입니다. Java가 '바이트 수를 저장합니까'입니까? 아니면 오프셋을 사용해야합니까?Java가 InputStream에서 읽기 위치를 저장합니까?

+1

오, 하나와주세요! 마지막 질문을 게시 한 후 문서를 읽고 시도해 볼 수 있습니다. 일부 데이터를 읽은 후에는 그 위치가 향상된다는 것을 알게 될 것입니다. 아마도 [I/O 튜토리얼] (http://download.oracle.com/javase/tutorial/essential/io/)을 확인하고 싶을 것입니다. –

답변

3

실제로 FileInputStream은 사용자의 위치를 ​​저장합니다.

System.out.println(fis.read()); 
System.out.println(fis.read()); 
System.out.println(fis.read()); 

윌 출력 : 호출

당신이 3 바이트, 0xff 0x00 0x0c을 가진 파일이있는 경우,

255 
0 
12 
2

에 대해 당신은 WhiteFang의 @ 반영하기 위해 무엇 쓰기 솔루션.

FileInputStream fis = new FileInputStream(files[0]); 
DataInputStream dis = new DataInputStream(new BufferedInputStream(fis)); 
int numFiles = dis.readInt(); 
int numBytesInName = dis.readInt(); 
String filename = dis.readUTF(); 
long numBytesInFile = dis.readLong(); 
// loop to read bytes into a byte[] 

현재, writeUTF/readUTF를 사용하면 파일 이름의 길이가 중복됩니다. 또한이 정보 뒤에 아무 것도 쓰지 않을 경우 파일 수를 기록 할 필요가 없습니다.

관련 문제