困扰了我三天的EOF错误

错误如下:

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully

解决办法: 

这句话是关键,先判断非不非空,再实例化!!!最后要感谢大佬帮我解答! 

 if (fis.available() > 0) {
                ois = new ObjectInputStream(fis);
                try {
                    accounts = (List) ois.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                accounts = null;
            }

@SuppressWarnings("unchecked")
	public List getFromDataBase() {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream("src/accounts.txt");
			if (fis.available() > 0) {
				ois = new ObjectInputStream(fis);
				try {
					accounts = (List) ois.readObject();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			} else {
				accounts = null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (ois != null)
					ois.close();
				if (fis != null)
					fis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return accounts;
	}

 

你可能感兴趣的:(java)