【发布时间】:2017-02-11 04:37:30
【问题描述】:
我已阅读并尝试关注
- How to remove the backslash in string using regex in Java?
- Java, Removing backslash in string object
- String replace a Backslash
还有很多博客,但都是为了从字符串中删除而不是为了属性文件
我正在尝试从属性文件值中删除 \ 但没有成功
这是我的config.properties 文件
query=select * from users
field=id
我的 Java 代码 A.java 是
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class A {
public static void main(String[] args) throws IOException {
String configPath = "/home/arif/util-test/config.properties";
Properties prop = new Properties();
FileInputStream configFileInputStream = new FileInputStream(configPath);
prop.load(configFileInputStream);
System.out.println("Property file loaded "+ configPath);
configFileInputStream.close();
String query = prop.getProperty("query");
query += " where " + prop.getProperty("field") + " = 1";
//query = query.replaceAll("\\\\", "");
query = query.replace("\\", "");
prop.replace("query", query);
prop.store(new FileOutputStream(configPath), null);
System.out.println("updated query="+ query);
}
}
并更新了config.properties 文件
#Mon Oct 03 14:34:27 IST 2016
query=select * from users where id \= 1
field=id
虽然我期待关注
#Mon Oct 03 14:34:27 IST 2016
query=select * from users where id = 1
field=id
并从终端或 cmd 获得预期输出,终端输出为
Property file loaded /home/arif/util-test/config.properties
updated query=select * from users where id = 1
您的帮助将不胜感激!谢谢
【问题讨论】:
-
@PeterLawrey,您在标记重复之前阅读过问题吗?
-
@PeterLawrey 这不是同一个主题,这里的问题是
=是属性文件中的特殊字符,因为它用于分割给定属性的键和值,例如它将始终使用反斜杠进行转义,但对于最终用户来说是完全透明的,因为调用getProperty("query")将返回不带反斜杠的值 -
为什么要删除反斜杠,因为它对您来说是透明的?这只是一个实现细节
-
谢谢@NicolasFilotto,我在想我不能用``但是当我再次加载它时它工作正常
标签: java properties-file