代码之家  ›  专栏  ›  技术社区  ›  Dnyanesh M

如何在Android中以编程方式更新“.properties”文件中的任何值?

  •  -1
  • Dnyanesh M  · 技术社区  · 7 年前

    在我的应用程序中,我在“assets”(“src/main/assets”)文件夹中手动创建了一个属性文件(“MyConfig.properties”)。此文件有一个条目,即。 DeviceUuid=xyzabc 现在我想将此值更新为 DeviceUuid=pqrstabc 为此,我写了下面的代码---

    public static void setProperty(String key, String value, Context context)
        {
            try
            {
                Properties properties = new Properties();
                OutputStream output = new FileOutputStream("MyConfig.properties");
                // set the properties value
                properties.setProperty(key, value);
    
                // save properties to project root folder
                properties.store(output, null);
    
                output.close();
            }
            catch (Exception ex)
            {
                Log.e(TAG, "Unable to WRITE Property "+key);
                Log.e(TAG, ""+ex);
            }
        }
    

    但我总是低于例外--

    java.io.FileNotFoundException: MyConfig.properties (Read-only file system)
    

    我对它做了一些研究,并在谷歌上尝试了不同的解决方案,但没有一个对我有效。

    public static String getProperty(String key, Context context)
        {
            try
            {
                Properties properties = new Properties();;
                AssetManager assetManager = context.getAssets();
                InputStream inputStream = assetManager.open("MyConfig.properties");
                properties.load(inputStream);
                return properties.getProperty(key);
            }
            catch (Exception ex)
            {
                Log.e(TAG, "Unable to READ Property "+key);
                Log.e(TAG, ""+ex);
                return null;
            }
        }
    

    请让我知道,如果我可以提供更多的细节相同。非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   CommonsWare    7 年前

    new FileOutputStream("MyConfig.properties"); 未指向有效位置。你需要提供一个 File internal storage , external storage ,或者 removable storage .