이진 파일에 일련의 다른 객체가 직렬화되었습니다. 끝까지 파일을 읽을 수있는 방법은 무엇입니까?끝까지 이진 파일을 읽으십시오.
try {
ObjectInputStream reader = new ObjectInputStream(new FileInputStream(fname));
Object obj = reader.readObject();
if (obj instanceof Azienda) {
Azienda a = (Azienda) obj;
company.put(a.getCod(), a);
} else if (obj instanceof Privato) {
Privato p = (Privato) obj;
privato.put(p.getCod(), p);
}
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} catch (FileNotFoundException ffe) {
System.err.println("Error: the file was not found!");
} catch (IOException ioe) {
ioe.printStackTrace();
}
이렇게하면 읽기 당 한 번만 개체를 읽습니다.
내가 텍스트 파일을 읽을 때 나는 당신이 예외라는 코드 냄새를 사용해야합니다 있도록 ObjectInputStream를 위해 과거를 읽을 때 예외 외에 더 EOF 체크가없는 null
하지만이 방법으로 파일을 읽을 때까지 무엇을해야합니까? – Mazzy
EOFException이 던져 질 때까지 파일의 끝을 알립니다. –