问题来自内部存储器中读取文件 [英] Issue with reading file from internal memory
问题描述
我米使用读&安培;写字符串到内部存储器。我的WriteFavData工作正常。但在ReadFavData不工作。其实,当我到达行如果(favFile.exists())在ReadFavData(..,..),它说没有找到与放文件;执行else部分。使用命令行shell访问的亚行外壳猫/data/data/com.android.mypackage/files/fav_data_channel 我已经检查仿真器内部存储器,该文件是存在的。为什么说文件不被present?任何想法....
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;
}
其实,我再次与放调试;跟踪在线如果(file.exists()),该文件不是present ..但是,如果我评论此行&功放;阅读我的文件直接,读音字能够成功地读取文件。什么错如果(file.exists())
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())?
推荐答案
favFile.exists()
总是返回假
在这里,因为它不会去您的私人数据目录进行检查。 openFileInput
或 openFileOutput
就行了。
favFile.exists()
always returns false
here, because it will not go to your private data directory to check it. openFileInput
or openFileOutput
will do.
删除它,并搭上了 FileNotFoundException异常
为 openFileInput
。
Remove it and catch a FileNotFoundException
for openFileInput
.
或者你也可以在 favFile
的名字您的私人数据目录添加路径。
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.
这篇关于问题来自内部存储器中读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!