简体   繁体   English

找不到文件将对象保存到电话文件目录

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

I am trying to write a object to file and retrieve it later from a different activity. 我正在尝试将对象写入文件,并稍后从其他活动中检索它。 When opening the Input stream to retrieve the object I get the IO exception: 当打开Input流以检索对象时,出现IO异常:

java.io.FileNotFoundException: /data/data/com.mooney.diveapp/files/savedDiveLocations: open failed: ENOENT (No such file or directory) java.io.FileNotFoundException:/data/data/com.mooney.diveapp/files/savedDiveLocations:打开失败:ENOENT(无此类文件或目录)

The String and File initialisation: 字符串和文件初始化:

String fileName="savedDiveLocations";
File theFileName; 


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

The code here returns a object from the file where it is saved previously: 此处的代码从文件先前保存的位置返回一个对象:

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){

And writing a object to file: 并将对象写入文件:

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){...

Any input appreciated. 任何输入表示赞赏。

Just executing theFileName= new File(objectConetxActivity.getFilesDir(), fileName); 只需执行theFileName= new File(objectConetxActivity.getFilesDir(), fileName); alone will not create your file. 单独不会创建您的文件。

You have to call theFileName.createNewFile(); 您必须调用theFileName.createNewFile(); . And make sure that method returns true. 并确保该方法返回true。 That means the file was effectively created. 这意味着文件已被有效创建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM