Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从.properties文件读取多个帐户的用户名和密码_Java_Properties File - Fatal编程技术网

Java 从.properties文件读取多个帐户的用户名和密码

Java 从.properties文件读取多个帐户的用户名和密码,java,properties-file,Java,Properties File,我有以下问题。我有一个configuration.properties文件,我想在我的应用程序中读取它。其形式如下: accountNames = account1, account2, account3 account1.userName = testUserName account1.password = testUserPassword account2.userName = secondTestUserName account2.password = secondTestUserPass

我有以下问题。我有一个configuration.properties文件,我想在我的应用程序中读取它。其形式如下:

accountNames = account1, account2, account3
account1.userName = testUserName
account1.password = testUserPassword
account2.userName = secondTestUserName
account2.password = secondTestUserPassword
account2.userName = thirdTestUserName
account2.password = thirdTestUserPassword
如何读取所有帐户并在HashMap中存储配对用户名和用户密码?在我看来,我有一个二维数组。我对访问帐户的每个属性的代码特别感兴趣

编辑:我已将configuration.properties文件更改为以下格式:

userNames = testUserName, secondTestUserName, thirdTestUserName
testUserName = testUserPassword
secondTestUserName = secondTestUserPassword
thirdTestUserName = thirdTestUserPassword
处理此问题的代码如下所示:

    properties.load(new FileInputStream(configFilePath));
    for(String s : properties.getProperty("userNames").split(",")){
        clientCredentials.put(s.trim(), properties.getProperty(s.trim()));
    }

    //test:
    for(String s:clientCredentials.keySet()){
        System.out.println("Key: "+s+" & value: "+clientCredentials.get(s));
    }
    final Map<String, String> accounts = new HashMap<String, String>();
    final File pwdFile = new File("path to your user/password file");
    BufferedReader br = null;
    try
    {
        br = new BufferedReader(new FileReader(pwdFile));

        br.readLine();// you don't need the first line

        while (true)
        {
            String  line = br.readLine();
            if (line == null)
                break;//end of file have been reached

            final String user = line.split("=")[1].trim();

            line = br.readLine();
            if (line == null)// pwd is missing
                throw new Exception("Invalid pwd file");

            final String pwd = line.split("=")[1].trim();
            accounts.put(user, pwd);

        }

    }
    catch (final Exception e)
    {
        // add your own error handling code here
        e.printStackTrace();
    }
    finally
    {
        if (br != null)
            br.close();

    }

谢谢您的帮助。

如果您只关心用户名和密码,请尝试以下操作:

    properties.load(new FileInputStream(configFilePath));
    for(String s : properties.getProperty("userNames").split(",")){
        clientCredentials.put(s.trim(), properties.getProperty(s.trim()));
    }

    //test:
    for(String s:clientCredentials.keySet()){
        System.out.println("Key: "+s+" & value: "+clientCredentials.get(s));
    }
    final Map<String, String> accounts = new HashMap<String, String>();
    final File pwdFile = new File("path to your user/password file");
    BufferedReader br = null;
    try
    {
        br = new BufferedReader(new FileReader(pwdFile));

        br.readLine();// you don't need the first line

        while (true)
        {
            String  line = br.readLine();
            if (line == null)
                break;//end of file have been reached

            final String user = line.split("=")[1].trim();

            line = br.readLine();
            if (line == null)// pwd is missing
                throw new Exception("Invalid pwd file");

            final String pwd = line.split("=")[1].trim();
            accounts.put(user, pwd);

        }

    }
    catch (final Exception e)
    {
        // add your own error handling code here
        e.printStackTrace();
    }
    finally
    {
        if (br != null)
            br.close();

    }

此代码假定包含username的行后面紧跟着一行,该行的密码为前一行的username。

您对不同的值使用相同的名称。这是不正确的。但是如果一个帐户有两个或两个以上的属性,我如何独立访问它们?为什么不只配对用户名和密码,比如firstUserName=firstUserPassword?我想到了这一点,并找到了一个例子,但是如果每个帐户都有两个以上的属性,例如添加帐户的电子邮件,你能做什么?您如何管理这种情况?您希望如何将其存储在HashMap中?如果要使用第一列作为键,则不正确。对于常规文本文件,这是一种很好的做法,但对于configuration.properties,我希望从properties类的loadInputStream inStream方法开始。因为您希望在HashMap中使用它,所以使用此代码更简单、更快。否则,必须使用using Properties.load加载文件,然后迭代Properties对象的每个元素。第二个优点是,您可以在加载文件修剪时应用一些自定义格式,大写。。。