我发现下面的代码不起作用,它会发出异常找不到文件。问题是什么以及如何解决?
try {
boolean exsit = xmlTools.isExist();
Log.d(TAG, "> 1 " + exsit);
} catch (Exception e) {
Log.d(TAG, "> 1 " + e.getMessage());
e.printStackTrace();
}
isExist()
方法如下所示:
public boolean isExist()throws Exception{
boolean flag=false;
FileInputStream fs= mContext.openFileInput(mConfigFile);
if( fs != null ){
flag=true;
}
return flag;
}
以下引发的例外情况:
( 4654):/data/data/com.demo.exmaple/files/appUsageD
ata.xml: open failed: ENOENT (No such file or directory)
答案 0 :(得分:1)
- 如果您使用的是root用户或模拟器,则可以/data/data/com.demo.exmaple/files/
中的file explorer
查看eclipse
,看看文件是否存在。
示例:强>
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TO WRITE TO INTERNAL STORAGE
try {
FileOutputStream fs = openFileOutput("vivek.txt", MODE_PRIVATE);
fs.write("hello".getBytes());
fs.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Reading from the file");
// TO READ FROM INTERNAL STORAGE
try {
FileInputStream fi = openFileInput("vivek.txt");
int i = 0;
while ((i = fi.read()) != -1) {
System.out.println((char)i);
}
fi.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}