java - android中的EBADF错误文件号错误

标签 java android properties

我正在尝试将 .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/

相关文章:

python - SA : can I have a 'year' column_property for a Date column?

javascript - 即使对象属性存在,Typescript 也无法访问它们

android - 如何保护/检测 Android 设备上的数据库还原?

java - Sonar Eclipse 插件使用错误的质量配置文件作为 Sonar 服务器

Java比较无序的ArrayLists

java - 使用什么作为 SQL 数据库日期的主键

android - 在 android 中存储不可序列化对象实例的最佳方法是什么?

android - 在 fragment 之间切换时清空 RecyclerView

command-line - 如何从命令行定义 gradle 中的 teamcity ['build.number' ] 属性

java - Java HttpPost 中序列化为请求参数的 json