gpt4 book ai didi

java - java方法保存的文件中添加了不需要的反斜杠

转载 作者:行者123 更新时间:2023-12-01 12:59:59 25 4
gpt4 key购买 nike

在我的 spring 项目中,我的一个服务类具有此方法来在磁盘中保存名为database.properties 的文件:

public void create_properties(String maquina, String usuario, String senha) {
System.out.println("create_properties");
Properties props = new Properties();

props.setProperty("jdbc.Classname", "org.postgresql.Driver");
props.setProperty("jdbc.url", "jdbc:postgresql://"+maquina+"/horario" );
props.setProperty("jdbc.user", usuario );
props.setProperty("jdbc.pass", senha );

props.setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
props.setProperty("hibernate.show_sql", "false");
props.setProperty("hibernate.hbm2ddl.auto", "validate");

FileOutputStream fos;
try {
fos = new FileOutputStream( "database.properties" );
props.store( fos, "propriedades" );
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

我的问题是属性 jdbc:url 应该是这样的:

jdbc:postgresql://localhost:5432/horario

但是保存的是这样的:

jdbc\:postgresql\://localhost\:5432/horario

谁能告诉我如何避免包含反斜杠?

最佳答案

它正在做完全正确的事情 - 您正在保存一个 properties 文件,该文件使用反斜杠转义冒号等内容。来自 documentation for Properties.store :

Then 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. 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.load 加载属性文件,您将在 Properties 对象中获取原始字符串。

如果您不想将值存储在属性文件中,请使用Writer并直接写入字符串。

关于java - java方法保存的文件中添加了不需要的反斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23581242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com