宿題をしようとして非常に悪い試みをした後、すべてを放棄して最初からやり直す方が速いと判断しました。すべてではありません...この部分は完全に機能したのでコピーしたので、変更する必要はありませんでした。完璧ではないかもしれませんが、うまくいきました。
ただし、テストのためにコンパイルすると、予期しないエラーが発生します。
Input error: java.io.EOFException
.
「入力エラー」は私のcatch(IOException ioe)
.
ファイル ( fileName
) は完全に空です。何も入っていません。何が原因でしょうか。ObjectInputStream
ファイルが空の場合に何もしないように指示する方法はありますか?
また、他の「反復」で空のファイルを使用してこれをテストしましたが、この問題はありませんでした。ファイルにも同じ名前を付けました。
public Repository (String fileName) throws FileNotFoundException,
IOException,
SecurityException,
ClassNotFoundException {
this.fileName = fileName;
this.clients = new ArrayList<Client> ();
FileInputStream fileIn = null;
ObjectInputStream in = null;
try {
fileIn = new FileInputStream(this.fileName);
in = new ObjectInputStream(fileIn);
this.clients = (ArrayList<Client>) in.readObject();
} catch (FileNotFoundException fnfe) {
System.out.println("File not found, error: " + fnfe);
} catch (IOException ioe) {
System.out.println("Input error: " + ioe);
} catch (ClassNotFoundException cnfe) {
System.out.println("Class not found, error: " + cnfe);
} catch (SecurityException se) {
System.out.println(
"You do not have permission to access this file, error: "
+ se);
} finally {
if (fileIn != null)
fileIn.close();
if (in != null)
in.close();
}