Java - 将文件位置写入属性文件

标签 java properties

我正在将 FILEPATH 参数写入属性文件,如下所示。

String newFilePath = txtFilepath.getText();
Properties prop = new Properties();
java.io.File propFile = new File("src/com/app/tool/properties/settings.properties");
prop.setProperty("FILEPATH", newFilePath);
System.out.println("newFilePath "+newFilePath);
   try {
     prop.store(new FileOutputStream(propFile), "New File location");
    } catch (IOException ex) {
     ex.printStackTrace();
    }

文件路径是D:\filelog.txt

在写入时,控制台打印如下..

newFilePath D:\filelog.txt

但是当我打开属性文件时,内容就像......

FILEPATH=D\:\\filelog.txt.

有人建议我,我在这里真正想要的是什么..?

最佳答案

在属性文件中,: 具有含义。

key = value
key: value

两者都有效。因此,在写入属性文件时必须对其进行转义。甚至 \ 也需要转义。但是当您在控制台上显示它们时,转义就会被识别,并且值会正确显示。

如果您看到 store() 的文档Properties 类的方法

Every entry in this Properties table is written out, one per line. For each entry the key string is written, then an ASCII =, then the associated element string. Each character of the key and element strings is examined to see whether it should be rendered as an escape sequence. The ASCII characters \, tab, form feed, newline, and carriage return are written as \, \t, \f \n, and \r, respectively. Characters less than \u0020 and characters greater than \u007E are written as \uxxxx for the appropriate hexadecimal value xxxx. For the key, all space characters are written with a preceding \ character. For the element, leading space characters, but not embedded or trailing space characters, are written with a preceding \ character. The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded.

注意:-如果您使用 Properties 读取属性文件(它将处理转义),那么应该不是问题,但是如果您读取属性像普通文本文件一样保存文件(除非你疯了,否则你不应该这样做),那么这将是一个问题。

关于Java - 将文件位置写入属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19269258/

相关文章:

java - 获取扫描仪类当前使用的分隔符

带有多个字符定界符的 Java String.split

Swift:access.property 和 access.method()

java - 安卓配置属性

java - 如何让构造函数安全?

java - 计算器字符串

java - 将 GUI 添加到现有的基于 Java 控制台的程序

java - 在运行时编辑 i18 属性

c# - 属性支持值范围

java - 在 Web 应用程序中找不到属性文件的路径?