未找到文件保存对象来电话文件目录 [英] File Not Found Saving Object to Phone File Directory

查看:124
本文介绍了未找到文件保存对象来电话文件目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个对象文件中,再从不同的活动进行检索。
当打开输入流检索对象我得到IO异常:

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:

java.io.FileNotFoundException:
  /data/data/com.mooney.diveapp/files/savedDiveLocations:打开失败:
  ENOENT(没有这样的文件或目录)

java.io.FileNotFoundException: /data/data/com.mooney.diveapp/files/savedDiveLocations: open failed: ENOENT (No such file or directory)

弦乐和文件初始化:

String fileName="savedDiveLocations";
File theFileName; 


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

在code这里返回从那里pviously节省$ P $该文件的对象:

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

和写入对象文件:

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

任何输入AP preciated。

Any input appreciated.

推荐答案

只是执行 theFileName =新的文件(objectConetxActivity.getFilesDir(),文件名); 本身并不能创造您的文件。

Just executing theFileName= new File(objectConetxActivity.getFilesDir(), fileName); alone will not create your file.

您必须调用 theFileName.createNewFile(); 。并确保该方法返回true。这意味着,该文件被有效创建。

You have to call theFileName.createNewFile();. And make sure that method returns true. That means the file was effectively created.

这篇关于未找到文件保存对象来电话文件目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆