困擾了我三天的EOF錯誤

錯誤如下:

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

解決辦法: 

這句話是關鍵,先判斷非不非空,再實例化!!!最後要感謝大佬幫我解答! 

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

@SuppressWarnings("unchecked")
	public List<Account> 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<Account>) 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;
	}

 

發佈了11 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章