Android方法openFileOutput获取“open failed:ENOENT”错误

时间:2014-01-03 06:13:06

标签: java android android-layout android-intent

我发现下面的代码不起作用,它会发出异常找不到文件。问题是什么以及如何解决?

    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)

1 个答案:

答案 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();
            }
        }

    }