0

I m using read & write string to internal memory. My "WriteFavData" is working fine. But In "ReadFavData" is not working. Actually when i reach the line if(favFile.exists()) in ReadFavData(..,..), it says file not found & execute the else part. I have already check the emulator internal memory using command line shell access adb shell cat /data/data/com.android.mypackage/files/fav_data_channel, the file is there. Why is saying file is not present.? Any ideas....

    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;
        }

Actually, i debugged again & track that on line if(file.exists()), the file is not present.. But if i comment this line & read my file directly, i m able to read file successfully. Whats wrong with if(file.exists())?

4

2 回答 2

2

favFile.exists()总是返回false这里,因为它不会去你的私有数据目录去检查它。openFileInput或者openFileOutput会做。

删除它并抓住一个FileNotFoundExceptionfor openFileInput

或者您可以在favFile名称之前添加私有数据目录的路径。

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

那将是真的。

于 2012-04-29T07:15:13.243 回答
0

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

FileOutputStream fos = ...
...
fos.getFD().sync();
于 2012-04-29T06:30:01.070 回答