我正在尝试将 .properties 文件写入我的 android 项目的 Assets 文件夹。我似乎正在加载文件并对其进行修改,但是当我去存储它时,它向我吐出这个错误:
12-10 21:19:07.447: W/System.err(1781): java.io.IOException: write failed: EBADF (Bad file number)
我已经添加了这样写的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
我发现的所有内容都表明 inputStream 必须以某种方式关闭,但我不知道如何关闭?
文件名为 config.properties 并且有 “积分=0” 作为唯一的数据。
这是我的代码(注意边滚动条)
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();
}
}
最佳答案
您不能使用该应用程序写入您的 res/raw 或 assets 文件夹。您将必须修改内容并将其存储在外部存储或内部文件系统或其他地方,但不能存储在 res/raw 或 assets 文件夹中。
关于java - android中的EBADF错误文件号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20509499/