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())?

2 Answers 2

2

favFile.exists() always returns false here, because it will not go to your private data directory to check it. openFileInput or openFileOutput will do.

Remove it and catch a FileNotFoundException for openFileInput.

Or you can add path to your private data directory before favFile name.

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

will be true then.

Sign up to request clarification or add additional context in comments.

1 Comment

You can return false; in exception catch.
0

try to call Filedescription.sync() after writing the file.

FileOutputStream fos = ... ... fos.getFD().sync(); 

5 Comments

After fos.getFD().sync(); i am getting error Invalid file length
could you provide the Logcat output
there is no log for this.. 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())
try to use if(file.canRead())
Hey Flo, file.canRead() also returns false. @Dot gave us a reason why this always return false.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.