如何从 java 中的属性值中删除反斜杠
How to remove the backslash from properties value in java
我已阅读并尝试关注
- 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
我们将不胜感激您的帮助!谢谢
您不能从属性文件中删除对 \
的使用,因为这是定义属性文件以实现的方式。
来自 Javadoc for Properties
All of these key termination characters may be included in the key by escaping them with a preceding backslash character; for example,
\:\=
这一行
query=select * from users where id = 1
无效,因为它包含两个未转义的 =
字符。第二个需要转义出来
您可以以不同的方式实现属性而不需要它,但它不会是标准的属性文件。
存储属性的替代方法是 XML、JSON 和 YAML。我更喜欢通常 simpler/cleaner 阅读的 YAML。
Yaml 规范 http://www.yaml.org/spec/1.2/spec.html
Java YAML 库 https://bitbucket.org/asomov/snakeyaml 这是一个很好的配置库。我将从这个开始。
我已阅读并尝试关注
- 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
我们将不胜感激您的帮助!谢谢
您不能从属性文件中删除对 \
的使用,因为这是定义属性文件以实现的方式。
来自 Javadoc for Properties
All of these key termination characters may be included in the key by escaping them with a preceding backslash character; for example,
\:\=
这一行
query=select * from users where id = 1
无效,因为它包含两个未转义的 =
字符。第二个需要转义出来
您可以以不同的方式实现属性而不需要它,但它不会是标准的属性文件。
存储属性的替代方法是 XML、JSON 和 YAML。我更喜欢通常 simpler/cleaner 阅读的 YAML。
Yaml 规范 http://www.yaml.org/spec/1.2/spec.html
Java YAML 库 https://bitbucket.org/asomov/snakeyaml 这是一个很好的配置库。我将从这个开始。