简体   繁体   中英

Android method openFileOutput get “open failed: ENOENT” error

I found below code does not work, and it will give out exception file not found . What's the problem and how to fix it?

    try {
            boolean exsit = xmlTools.isExist();
            Log.d(TAG, "> 1 "  + exsit);

        } catch (Exception e) {
            Log.d(TAG, "> 1 "  + e.getMessage());
            e.printStackTrace();
        }

The isExist() method is like below:

public boolean isExist()throws Exception{ 
    boolean flag=false; 
    FileInputStream fs=  mContext.openFileInput(mConfigFile); 
    if( fs != null ){ 
        flag=true; 
    } 

    return flag; 
} 

The exception thrown below:

( 4654):/data/data/com.demo.exmaple/files/appUsageD
ata.xml: open failed: ENOENT (No such file or directory)

- If you are on your rooted phone or emulator, then you can look into /data/data/com.demo.exmaple/files/ from file explorer in eclipse to see if actually the file exists or not .

Example:

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM