簡體   English   中英

找不到文件將對象保存到電話文件目錄

[英]File Not Found Saving Object to Phone File Directory

我正在嘗試將對象寫入文件,並稍后從其他活動中檢索它。 當打開Input流以檢索對象時,出現IO異常:

java.io.FileNotFoundException:/data/data/com.mooney.diveapp/files/savedDiveLocations:打開失敗:ENOENT(無此類文件或目錄)

字符串和文件初始化:

String fileName="savedDiveLocations";
File theFileName; 


theFileName= new File(objectConetxActivity.getFilesDir(), fileName);    

此處的代碼從文件先前保存的位置返回一個對象:

String fileAddress = objectConetxActivity.getFilesDir()+ fileName;
        ObjectInputStream ois = null;
        FileInputStream streamIn=null;
        Object locationObject = null;

        try{

            // except thrown here
            streamIn = new FileInputStream(fileAddress); // address of file
            ois = new ObjectInputStream(streamIn);
            locationObject = ois.readObject();
        }catch (Exception e){

並將對象寫入文件:

ObjectOutputStream oos = null;
    FileOutputStream fout = null;


    // save to internal durectpry as opposed to exteranl SD card
    try{

        String fileAddress = objectConetxActivity.getFilesDir()+ fileName;
        fout = new FileOutputStream(fileAddress); // filepath
        oos = new ObjectOutputStream(fout);
        oos.writeObject(mapLocationsObject);

    }catch(Exception ex){...

任何輸入表示贊賞。

只需執行theFileName= new File(objectConetxActivity.getFilesDir(), fileName); 單獨不會創建您的文件。

您必須調用theFileName.createNewFile(); 並確保該方法返回true。 這意味着文件已被有效創建。

暫無
暫無

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

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