Android 从内存读取文件时出现问题

Android 从内存读取文件时出现问题,android,Android,我使用读写字符串到内存。我的“WriteFavData”工作正常。但是在中,“ReadFavData”不起作用。实际上,当我到达ReadFavData(..,..)中的if(favFile.exists())行时,它说找不到文件并执行else部分。我已经使用命令行shell access检查了模拟器的内部内存,adb shell cat/data/data/com.android.mypackage/files/fav_data_channel,文件就在那里。为什么说文件不存在。?有什么想法吗

我使用读写字符串到内存。我的“WriteFavData”工作正常。但是在中,“ReadFavData”不起作用。实际上,当我到达ReadFavData(..,..)中的if(favFile.exists())行时,它说找不到文件并执行else部分。我已经使用命令行shell access检查了模拟器的内部内存,adb shell cat/data/data/com.android.mypackage/files/fav_data_channel,文件就在那里。为什么说文件不存在。?有什么想法吗

    public boolean ReadFavData(Context context, String channelName)
        {
            boolean isFileFound = false;
            FileInputStream fin ;
            StringBuffer strContent = new StringBuffer("");
            String fileName = FAV_FILE_NAME + "_" + channelName;
            File favFile = new File(fileName);
            int ch;
            if(favFile.exists())
            {
                isFileFound = true;
                try {
                    fin = context.openFileInput(FAV_FILE_NAME + "_" + channelName);
                    isFileFound = true;
                    while ((ch = fin.read()) != -1)
                        {
    strContent.append((char) ch);
    }

                    fin.close();                


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }       
            }
            else 
            {
                Log.i(TAG, "not found");
            }

            return isFileFound;
        }



public boolean WriteFavData(Context context, String channelName)
        {
            String favStr;
            FileOutputStream fos ;
            JSONObject videos = new JSONObject();           
            videos.put("videos", favJsonArray);
            String favStr = videos.toJSONString(); //note.toString();
            String fileName = FAV_FILE_NAME + "_" + channelName;
            File f = new File(fileName);
            if(f.exists())
            {
                Log.i("Old FIle", "Found");
                f.delete();     
            }

            if(f.exists())
                Log.i("Still", "Exist");

            try {               
                    fos = context.openFileOutput(FAV_FILE_NAME + "_" + channelName, Context.MODE_PRIVATE);              
                fos.write(favStr.getBytes());
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
            return true;
        }

实际上,我再次调试并在行跟踪该文件,如果(file.exists()),则该文件不存在。。但若我注释这行并直接读取我的文件,我就能够成功地读取文件。如果(file.exists())有什么问题?

在写入文件后尝试调用Filedescription.sync()

FileOutputStream fos = ...
...
fos.getFD().sync();
favFile.exists()
始终在此处返回
false
,因为它不会转到您的私有数据目录进行检查
openFileInput
openFileOutput
即可

删除它并捕获
openFileInput
FileNotFoundException

或者,您可以在
favFile
name之前将路径添加到您的私有数据目录中

new File("/data/data/com.android.mypackage/files/fav_data_channel").exists();

然后将为真。

在fos.getFD().sync()之后;我收到错误无效的文件长度你能提供Logcat输出吗这里没有这方面的日志。。实际上,我又调试了一次&如果(file.exists())文件不存在,请在线跟踪它。。但若我注释这行并直接读取我的文件,我就能够成功地读取文件。if(file.exists())有什么问题?尝试使用
if(file.canRead())
嘿,Flo,file.canRead()也返回false@Dot给了我们一个为什么总是返回false的原因在异常捕获中。