2010-08-10 21 views
1

我想寫一個對象(pilotRecord)到一個文件並再次讀取它。我知道我不需要指定一個路徑,因爲它是我的應用程序的內部路徑,所以如果應用程序被卸載,我希望所有文件都被刪除。FileNotFoundException當試圖讀取我寫的文件

這裏是我的代碼:

fileoutputstream = openFileOutput("test1", Context.MODE_WORLD_WRITEABLE); 
    Log.d(this.getClass().getName(), "loadPilotRecord: "+fileoutputstream.toString()); 
    objectoutputstream = new ObjectOutputStream(fileoutputstream); 
    Log.d(this.getClass().getName(), "loadPilotRecord: "+objectoutputstream.toString()); 
    objectoutputstream.writeObject(pilotRecord); 
    objectoutputstream.close(); 
    fileoutputstream.close(); 

    fileinputstream = new FileInputStream("test1"); 
    Log.d(this.getClass().getName(), "loadPilotRecord: "+fileinputstream.toString()); 
    objectinputstream = new ObjectInputStream(fileinputstream); 
    Log.d(this.getClass().getName(), "loadPilotRecord: "+objectinputstream.toString()); 
    pilotRecord = (PilotRecord)objectinputstream.readObject(); 
    objectinputstream.close(); 
    fileinputstream.close(); 

我的問題是,我得到了上面的代碼上以下行FileNotFoundException異常: 的FileInputStream =新的FileInputStream(「測試1」); 我真的不知道如何找出它使用的路徑,或者有一個更明顯的問題,我只是沒有看到。 對不起,如果這是一個基本的,但我仍然試圖找到我的腳。 Log.d語句只輸出類名和Id。

TIA,

  • 弗林克

回答

2

您是否嘗試過openfileinput(「測試1),而不是新的FileInputStream(」測試1" )

+0

乾杯。這工作很好吃,謝謝。不知道爲什麼 new FileInputStream(「test1」) 無法正常工作,因爲它是從示例中複製的 – FrinkTheBrave 2010-08-10 16:29:47

+0

文檔易出錯:P 我很高興它有幫助! :) – plagelao 2010-08-11 09:13:18

1

要找出哪些路徑實際上是用來嘗試:

File f = new File("test1"); 
Log.d(this.getClass().getName(), f.getAbsolutePath()); 

看這個位置,如果真的創建的文件 - 如果不是,你將無法閱讀。

編輯:刪除平齊猜測這是相當長的一段廢話

+0

-1 - 你*不*需要什麼?在關閉它之前刷新一個流,關閉會執行一次刷新,無論如何,這不會導致FileNotFoundException,因爲該文件將由FileOutputStream構造函數調用創建。 – 2010-08-10 14:51:27

+0

返回/ test1,所以我認爲它是相對於該應用程序,這是有道理的。不要以爲我可以從模擬器內訪問該文件區域,這是一個恥辱 – FrinkTheBrave 2010-08-10 16:33:13

相關問題