0
を読んだとき、私はNullPointerExceptionが避けることができ、私はお客様Javaの - 私は空のファイル
のクラスカスタマーと配列のリストを持っていると私はオブジェクトを読み取るための方法を持っているどのように顧客のファイルを形成し、それらを追加しますアレイリストへ
import java.util.*;
import java.io.*;
public void readFile(File customerFile) throws IOException, NullPointerException {
ObjectInputStream OIS = null;
try {
FileInputStream FIS = new FileInputStream(customerFile);
OIS = new ObjectInputStream(FIS);
Customer customerObj = null;
while (true) {
customerObj = (Customer) OIS.readObject();
this.customerList.add(customerObj);
}
} catch (Exception ex) {
System.out.println("(Exception : " + ex.toString() + ")");
} finally {
OIS.close();
if (!isEmpty()) {
Print(); // print the customerList
} else {
System.out.println("The Customer List Is Empty !");
}
}
}
だから私を助けることができますか?私はtry-with-resources
文を使用しています私のコードでは
public void readFile(File customerFile) throws IOException, NullPointerException {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(customerFile))) {
Customer customerObj = null;
while (true) {
customerObj = (Customer) ois.readObject();
this.customerList.add(customerObj);
}
} catch (Exception ex) {
System.out.println("(Exception : " + ex.toString() + ")");
}
if (!isEmpty()) {
Print(); // print the customerList
} else {
System.out.println("The Customer List Is Empty !");
}
}
:あなたはこのようなコードを使用することができますOIS.close();
'FileName.length()== 0'空のファイルについては、この条件を確認してください。 –