Java属性反斜杠

Java属性反斜杠,java,properties,Java,Properties,我正在使用Java属性读取属性文件。一切都很好,但属性会悄悄地删除反斜杠 (即) 如何使属性不这样做 我正在使用代码prop.getProperty(键) 我从文件中获取属性,我希望避免添加双反斜杠使用双反斜杠c:\\sdjf\\slkdfj.jpg Properties props = new Properties(); props.setProperty("test", "C:\\dev\\sdk\\test.dat"); System.out.println(props.getProper

我正在使用Java属性读取属性文件。一切都很好,但属性会悄悄地删除反斜杠

(即)

如何使属性不这样做

我正在使用代码
prop.getProperty(键)


我从文件中获取属性,我希望避免添加双反斜杠使用双反斜杠
c:\\sdjf\\slkdfj.jpg

Properties props = new Properties();
props.setProperty("test", "C:\\dev\\sdk\\test.dat");
System.out.println(props.getProperty("test"));    // prints C:\dev\sdk\test.dat
更新归功于下面的@ewh。显然,Windows可以识别前面的斜杠。所以,我想你可以让你的用户用前斜杠来代替,如果你以后需要反斜杠,你可以做一个替换。我在下面测试了这段代码,效果很好

Properties props = new Properties();
props.setProperty("test", "C:/dev/sdk/test.dat");
System.out.println(props.getProperty("test"));   // prints C:/dev/sdk/test.dat
正是这导致了您所看到的问题,因为反斜杠用于特殊目的

保存所有数据的逻辑行 对于一个键元素,可以展开一对 穿过几个相邻的自然保护区 通过转义行终止符来创建行 带有反斜杠字符的序列, \

如果您无法使用CoolBeans的建议,那么您可以做的是预先将属性文件读取为字符串,并将反斜杠替换为双反斜杠,然后将其馈送到Properties.load()中


使用前斜杠。在Java中,永远不需要在文件名中使用反斜杠。

如果您确实需要在将要加载的属性文件中使用反斜杠(例如,对于不是文件路径的属性)为每个反斜杠字符放置
\u005c

如@unhillbilly提供的文档所示,在属性文件中特别处理反斜杠

@EJP:例如,如果您希望在属性文件中存储NTLM登录id,其中格式为带反斜杠的
DOMAIN\USERNAME
,则肯定需要反斜杠。此类型的属性不是文件名,因此前斜杠不起作用

编辑:@Max Nanasy:来自上述文档()(强调我的)

该方法不会将无效转义字符之前的反斜杠字符“\”视为错误;反斜杠会自动删除。例如,在Java字符串中,序列“\z”将导致编译时错误。相反,此方法会自动删除反斜杠。因此,此方法将两个字符序列“\b”视为等同于单个字符“b”


对我来说,除非我指定了unicode,否则属性文件中的反斜杠(即使是双反斜杠“\\”)总是有问题。

以下代码将有所帮助:

BufferedReader metadataReader = new BufferedReader(new InputStreamReader(new FileInputStream("migrateSchemaGenProps.properties")));
Properties props = new Properties();
props.load(new StringReader(IOUtils.getStringFromReader(metadataReader).replace("\\", "/")));

在属性文件中使用反斜杠并不是一件好事,因为它们是转义字符

然而:Windows用户将倾向于在任何路径中使用它们。。。因此,在一行中,感谢apache common IO:

params.load(new StringReader(IOUtils.toString(paramFile.toURI(), null).replaceAll("\\\\", "/")));

除了Bala R的答案之外,我还有以下解决方案,可以在一行末尾保留反斜杠的换行语义

这是我的密码:

private static Reader preparePropertyFile(File file) throws IOException {

    BufferedReader reader = new BufferedReader(new FileReader(file));
    StringBuilder result = new StringBuilder();

    String line;
    boolean endingBackslash = false;

    while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (endingBackslash) {

            // if the line is empty, is a comment or holds a new property
            // definition the backslash found at the end of the previous
            // line is not for a multiline property value.
            if (line.isEmpty()
                    || line.startsWith("#")
                    || line.matches("^\\w+(\\.\\w+)*=")) {

                result.append("\\\\");
            }
        }

        // if a backslash is found at the end of the line remove it
        // and decide what to do depending on the next line.
        if (line.endsWith("\\")) {
            endingBackslash = true;
            line = line.substring(0, line.length() - 1);
        } else {
            endingBackslash = false;
        }
        result.append(line.replace("\\", "\\\\"));
    }
    if (endingBackslash) {
        result.append("\\\\");
    }
    return new StringReader(result.toString());
}

private static Properties getProperties(File file) throws IOException {
    Properties result = new Properties();
    result.load(preparePropertyFile(file));
    return result;
}

\
替换为
\
,如下所示:

c:\sdjf\slkdfj.jpg 


三次使用反斜杠得到一个: 例如: key=value1\\value2 在属性文件中,将转到 key=value1\value2
在java属性对象中

我希望避免使用双反斜杠,因为我是从用户编写的文件中获取属性的。很抱歉,您最初的帖子中没有这些信息。如果您使用的是windows/DOS文件系统,那么恐怕您运气不好。您在windows系统上并不是运气不好。实际上,您可以在windows系统的路径名中使用正斜杠。我通常在java属性文件中这样做,以避免与反斜杠混淆。还要注意的是,在Windows中可以对路径名使用普通的“/”斜杠。为什么要使用
\u005c
而不是“\\”?它的可读性较低,而且更容易键入。另外,我如何在堆栈交换注释中设置“\\\”的格式?@MaxNanasy使用反勾号
//
@Alex,它适用于正向斜杠(
/
),但我似乎无法让它使用反斜杠(```)。@Max Nanasy:OP询问如何从文件加载属性,不通过Java字符串文本设置属性。引用的文档用于加载属性文件。您需要一个replaceAll,因为属性中可能有几个反斜杠-file@OlivierFaucheux那是错误的@奥利维尔·福谢,那是错的。请参见:
replace
替换所有引用
replaceAll
将不起作用,因为“\\”不是有效的正则表达式。将不再识别多行属性值。请参阅我的帖子以获得解决方案。
private static Reader preparePropertyFile(File file) throws IOException {

    BufferedReader reader = new BufferedReader(new FileReader(file));
    StringBuilder result = new StringBuilder();

    String line;
    boolean endingBackslash = false;

    while ((line = reader.readLine()) != null) {
        line = line.trim();
        if (endingBackslash) {

            // if the line is empty, is a comment or holds a new property
            // definition the backslash found at the end of the previous
            // line is not for a multiline property value.
            if (line.isEmpty()
                    || line.startsWith("#")
                    || line.matches("^\\w+(\\.\\w+)*=")) {

                result.append("\\\\");
            }
        }

        // if a backslash is found at the end of the line remove it
        // and decide what to do depending on the next line.
        if (line.endsWith("\\")) {
            endingBackslash = true;
            line = line.substring(0, line.length() - 1);
        } else {
            endingBackslash = false;
        }
        result.append(line.replace("\\", "\\\\"));
    }
    if (endingBackslash) {
        result.append("\\\\");
    }
    return new StringReader(result.toString());
}

private static Properties getProperties(File file) throws IOException {
    Properties result = new Properties();
    result.load(preparePropertyFile(file));
    return result;
}
c:\sdjf\slkdfj.jpg 
c:\\sdjf\\slkdfj.jpg