简体   繁体   中英

EBADF bad file number error in android

I am attempting to write a .properties file to the assets folder of my android project. I seem to be loading the file and modifying it fine, but when I go to store it spits this error at me:

12-10 21:19:07.447: W/System.err(1781): java.io.IOException: write failed: EBADF (Bad file number)

I have added the permissions to write like:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Evrything I find says the inputStream must somehow be closed, but I dont see how?

File name is config.properties and has "Points=0" as the only data.

Here is my code(note side scrollbar)

public static void write(Context Activity){ // the activity passed in is "this" from the caller, who is an activity
    AssetManager am = Activity.getResources().getAssets();
    Properties pp = new Properties();
    InputStream isConfig = null;
    try {
        isConfig = am.open("config.properties",Context.MODE_PRIVATE);
        pp.load(isConfig);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    pp.setProperty("Points", "1");//This key exists
    try {
        pp.store(Activity.getAssets().openFd("config.properties").createOutputStream(), null); //This is the problem
        isConfig.close();
    } catch (FileNotFoundException e) {
        System.err.println("file not found");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IO Exception");
        e.printStackTrace();
    }
}

You cannot write to your res/raw or assets folder using the application. You will have to modify the contents and store it in external storage or internal file system or somewhere else but not in res/raw or assets folder.

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