简体   繁体   English

在特定行上写入属性文件

[英]write to properties file on specific line

Please let me know if this question has been asked before. 请让我知道是否曾经问过这个问题。

The Goal 目标

In my android application when a user launches the App it loads the Login.class first. 在我的Android应用程序中,当用户启动应用程序时,它将首先加载Login.class。 This class checks to see if a local file (in the included file path of the App) called app_prefs.prop exists (which it does) and then it checks the following fields structured like so: 此类检查是否存在一个名为app_prefs.prop的本地文件(在App的包含文件路径中)(它确实存在),然后检查以下结构如下的字段:

username=
user_hash=
saved_email=
email_hash=

Those fields are blank by default just like so. 默认情况下,这些字段为空白。 I am reading them using the following code: 我正在使用以下代码阅读它们:

public static String getConfigValue(Context context, String name) {
    Resources resources = context.getResources();
    String TAG = "Retrieve";
    try {
        InputStream rawResource = resources.openRawResource(R.raw.app_prefs);
        Properties properties = new Properties();
        properties.load(rawResource);
        return properties.getProperty(name);
    } catch (Resources.NotFoundException e) {
        Log.e(TAG, "Unable to find the config file: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "Failed to open config file.");
    }
    return null;
}

If they return empty values, which by default they will, then the login screen is showed. 如果它们返回空值,默认情况下将返回空值,则显示登录屏幕。 If they do not, the login is attempted by default and if successful it will continue to the App, if not, login is shown again of course. 如果不这样做,则默认情况下将尝试登录,如果成功,登录将继续到应用程序;如果未成功,则当然会再次显示登录。

The Issue 问题

When they sign in, I want to write the data into those fields. 当他们登录时,我想将数据写入这些字段。 Currently its being sent to and from the server using JSON and works awesome. 目前,它是使用JSON发送到服务器或从服务器发送的,并且效果很棒。 I am able to extract this data as well to a string variable which I am then passing to my save to config file after logging the user in but before continuing to the next App screen. 我也可以将这些数据提取到字符串变量中,然后在登录用户后继续下一步操作之前将其传递到保存到配置文件中。 This is where the problem lies, I have enabled the permission 这是问题所在,我已启用权限

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

I have also passed all the values to it, but they are not being written to the lines I want them too. 我也将所有值传递给它,但是也没有将它们写入我想要的行中。 Here is the code I am using to write the file with: 这是我用来写入文件的代码:

private void commitUserInfotoFile(Context context, String username, String passhash, String rmemail, String rmemailhash) {
    Resources resources = context.getResources();
    String TAG = "Store";
    try {
        InputStream rawResource = resources.openRawResource(R.raw.app_prefs);
        Properties properties = new Properties();
        properties.load(rawResource);
        //tried using setProperty as well as put but neither works
        properties.setProperty("username", username);
        properties.put("username", username);
        properties.setProperty("user_hash", passhash);
        properties.setProperty("saved_email", rmemail);
        properties.setProperty("email_hash", rmemailhash);
        Log.e(TAG, "Wrote the values to the stored file");
    } catch (Resources.NotFoundException e) {
        Log.e(TAG, "Unable to find the config file: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "IO Exception loading file.");
    }
}

Yet its not storing those values to the file even tho I get the message Wrote the values to the stored file in my console. 但是,即使我收到消息Wrote the values to the stored file控制台中Wrote the values to the stored file ,它也不会将这些值Wrote the values to the stored file中。 I am a little confused as to the writing of properties using this method so any help would be appreciated. 对于使用此方法编写属性,我有些困惑,因此不胜感激。 Thanks 谢谢

You never store the result of your edits back into the resource. 您永远不会将编辑结果存储回资源中。 setProperty() just updates some internal key-value pair in the Properties object, it does not update it's source. setProperty()只会更新Properties对象中的一些内部键值对,而不会更新其源。 You need to call Properties.store(OutputStream, String) when you are done with your edits. 完成编辑后,需要调用Properties.store(OutputStream,String)。 See here: 看这里:

https://developer.android.com/reference/java/util/Properties.html#store(java.io.OutputStream, java.lang.String)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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