"HashMap"종류의 객체를 파일에 쓰려고 시도하고 있습니다. & 프로그램을 다시 실행할 때이를 복구하십시오. 하지만 그 개체를 읽을 때 개체가 파일에서 읽히지 않을 때 EOFException이 발생했습니다. FileOutputStream & ObjectOutputStream에 대한 객체를 작성할 때 flush() & close() 메서드를 사용합니다. 또한 내 파일에 대해 OutputStream & InputStream을 함께 만듭니다. 여기 내 코드입니다 :파일에 쓰는 동안 Java Object Stream에 문제가 발생했습니다.
DataOutputStream outToFile;
DataInputStream inFromFile;
ObjectOutputStream writeTableToFile;
ObjectInputStream readTableFromFile;
File tableFile;
public DNS(){
try {
tableFile = new File("table.txt");
outToFile = new DataOutputStream(new FileOutputStream(tableFile));
writeTableToFile = new ObjectOutputStream(outToFile);
inFromFile = new DataInputStream(new FileInputStream(tableFile));
readTableFromFile = new ObjectInputStream(inFromFile);
HashMap table2 = (HashMap) readTableFromFile.readObject();
if (table2 == null)
table=new HashMap(100);
else
table = table2;
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(EOFException e){
table=new HashMap(100);
}
catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
여기에 쓰는 객체에 대한 코드입니다
같은 파일에 대한 입력 스트림과 동시에 출력 스트림이 동시에 열리나요? 이것은 좋은 생각입니까? 읽고 읽으려고 할 때 입력 스트림을 열고 닫으십시오. 출력 스트림을 작성하여 열어 쓸 때 닫은 다음 닫으십시오. –