【问题标题】:value is not getting set properly in properties file属性文件中的值未正确设置
【发布时间】:2018-02-18 15:40:58
【问题描述】:

我有一个属性文件(ab.properties),其值如下:

颜色=橙色

storeLocation=./test.json

公司=苹果

我想将storeLocation的值修改为C:\Users\kumar\testFiles\test.json

在代码中,file 是我读取 ab.properties 文件的路径,storelocation1 包含路径 C:\Users\kumar\testFiles\test.json(我想在 storelocation 中更新)。见以下代码:

try (InputStream in = new FileInputStream(file)) {
            Properties prop = new Properties();
            prop.load(in);
            in.close();
            prop.setProperty("storeLocation", storeLocation1);
            OutputStream out = new FileOutputStream(file);
            prop.store(out, null);
            out.close();


        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

通过使用上面的代码,我得到以下输出:

颜色=橙色

storeLocation=C\:\\Users\\kumar\\testFiles\\test.json

公司=苹果

storeLocation 的值正在更新,但是

我想要 C: 而不是 C\: 。有人可以指导我吗?

【问题讨论】:

  • "我想要 C: 而不是 C:" ?这是什么意思?
  • 哎呀,当我发布此查询时,似乎没有正确使用反斜杠字符。我得到的输出为
  • storeLocation=C\:\\Users\\kumar\\testFiles\\test.json
  • 所以我想要 C: 而不是 C\:

标签: java properties fileinputstream fileoutputstream properties-file


【解决方案1】:

您可以使用以下方法来准备属性文件:

public static Properties load(File file) throws IOException {
    try (BufferedReader in = new BufferedReader(new FileReader(file))) {
        int pos;
        String line;
        Properties properties = new Properties();

        while ((line = in.readLine()) != null) {
            if (!line.startsWith("#")) {
                if ((pos = line.indexOf('=')) > 0) {
                    String key = line.substring(0, pos).trim();
                    String val = line.substring(pos + 1).trim();
                    properties.setProperty(key, val);
                }
            }
        }

        return properties;
    }
}

并保存属性文件:

public static Properties store(File file, Properties properties) throws IOException {
    try (BufferedWriter out = new BufferedWriter(new FileWriter(file, false))) {
        Enumeration<String> names = (Enumeration<String>)properties.propertyNames();

        while (names.hasMoreElements()) {
            String key = names.nextElement();
            String val = properties.getProperty(key);
            out.write(String.format("%s = %s", key, val));
            out.newLine();
        }
    }

    return properties;
}

在这种情况下,Properties 和文件中的字符串完全相同:C:\Users\kumar\testFiles\test.json。 我不知道它如何与转义符号一起使用,但也许这个例子给了你和想法。

在现实生活中,如果我有这样的要求,我会覆盖BuffereReaderBufferedWriter,使用标准的Properties.load()Properties.store()方法。

【讨论】:

  • 我已经使用了你的代码,但我仍然得到相同的输出。即 C\:\\Users\\kumar\\testFiles\\test.json
【解决方案2】:

目前,我正在使用以下解决方案,目前对我有效:

    try (InputStream in = new FileInputStream(file)) {
        properties prop = new Properties();
        prop.load(in);
        in.close();
        prop.setProperty("storeLocation", storeLocation1);
        OutputStream out = new FileOutputStream(file);
        prop.store(out, null);
        out.close();


    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  updateString(file);
}

    public static void updateString(File file) throws IOException {
        Path path = Paths.get(file.toString());
        Charset charset = StandardCharsets.UTF_8;

        String content = new String(Files.readAllBytes(path), charset);
        content = content.replace("\\" + ":", ":");
        Files.write(path, content.getBytes(charset));

    }

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-29
    • 2019-03-22
    相关资源
    最近更新 更多