簡體   English   中英

嘗試讀取我編寫的文件時出現FileNotFoundException

[英]FileNotFoundException when trying to read a file I've written

我正在嘗試將對象(pilotRecord)寫入文件並再次讀回。 我知道我不需要指定路徑,因為它是應用程序內部的路徑,因此,如果卸載該應用程序,則希望刪除所有文件。

這是我的代碼:

    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 = new FileInputStream(“ test1”); 我不太確定如何找出正在使用的路徑,或者可能還有一個更明顯的問題,我只是沒有看到。 抱歉,這有點基本,但是我仍在努力尋找自己的腳。 Log.d語句僅輸出類名稱和ID。

TIA,

  • 弗林克

您是否嘗試過使用openfileinput(“ test1)而不是新的FileInputStream(” test1“)?

要找出實際使用的路徑,請嘗試:

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

如果確實創建了文件,請查看此位置-否則,您將無法讀取。

編輯:用沖洗消除了猜測,這有點廢話

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM