从java中的属性文件读取正则表达式
我在从java的属性文件中读取像从java中的属性文件读取正则表达式,java,regex,properties,Java,Regex,Properties,我在从java的属性文件中读取像(\+?\s*[0-9]+\s*)+这样的值时遇到问题,因为使用getProperty()方法得到的值是(+?s*[0-9]+s*)+ 对属性文件中的值进行转义还不是一个选项 有什么想法吗?只需使用经典的缓冲读取器阅读即可: final URL url = MyClass.class.getResource("/path/to/propertyfile"); // check if URL is null; String line; try ( fin
(\+?\s*[0-9]+\s*)+
这样的值时遇到问题,因为使用getProperty()
方法得到的值是(+?s*[0-9]+s*)+
对属性文件中的值进行转义还不是一个选项
有什么想法吗?只需使用经典的
缓冲读取器阅读即可:
final URL url = MyClass.class.getResource("/path/to/propertyfile");
// check if URL is null;
String line;
try (
final InputStream in = url.openStream();
final InputStreamReader r
= new InputStreamReader(in, StandardCharsets.UTF_8);
final BufferedReader reader = new BufferedReader(r);
) {
while ((line = reader.readLine()) != null)
// process line
}
如果有必要,可以适应Java 6…我认为这个类可以解决属性文件中的反斜杠问题
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
public class ProperProps {
HashMap<String, String> Values = new HashMap<String, String>();
public ProperProps() {
};
public ProperProps(String filePath) throws java.io.IOException {
load(filePath);
}
public void load(String filePath) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().length() == 0 || line.startsWith("#"))
continue;
String key = line.replaceFirst("([^=]+)=(.*)", "$1");
String val = line.replaceFirst("([^=]+)=(.*)", "$2");
Values.put(key, val);
}
reader.close();
}
public String getProperty(String key) {
return Values.get(key);
}
public void printAll() {
for (String key : Values.keySet())
System.out.println(key +"=" + Values.get(key));
}
public static void main(String [] aa) throws IOException {
// example & test
String ptp_fil_nam = "my.prop";
ProperProps pp = new ProperProps(ptp_fil_nam);
pp.printAll();
}
}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.HashMap;
公共类财产权{
HashMap值=新的HashMap();
公共财产计划{
};
公共属性操作(字符串文件路径)引发java.io.IOException{
加载(文件路径);
}
公共无效加载(字符串文件路径)引发IOException{
BufferedReader reader=新BufferedReader(新文件读取器(文件路径));
弦线;
而((line=reader.readLine())!=null){
if(line.trim().length()==0 | | line.startsWith(“#”)
继续;
String key=line.replaceFirst(([^=]+)=(.*),“$1”);
字符串val=line.replaceFirst(([^=]+)=(.*),“$2”);
value.put(key,val);
}
reader.close();
}
公共字符串getProperty(字符串键){
返回值.get(key);
}
public void printAll(){
for(字符串键:Values.keySet())
System.out.println(key+“=”+Values.get(key));
}
公共静态void main(字符串[]aa)引发IOException{
//示例与测试
字符串ptp_fil_nam=“my.prop”;
ProperProps pp=新的ProperProps(ptp_fil_nam);
pp.printAll();
}
}
我回答这个问题已经很晚了,但也许这可以帮助其他最终来到这里的人
较新版本的Java(不确定是哪一个,我使用的是8)通过使用\
来表示我们习惯使用的正常\
来支持值转义
例如,在您的情况下,(\\+?\\s*[0-9]+\\s*)+
是您正在寻找的内容。还不是选项吗?你什么意思?还不是选项
!如果除此之外没有其他方法有效,那么它将是一个选项。反斜杠在属性文件中具有特殊意义。例如,它允许一个条目在下一行继续。解决这个问题是不可能的。这是可行的,但这有什么意义呢?如果您决定为此使用属性文件,那么为什么要以使其无效的方式编写它们?如果你决定做一些自定义的事情,不要叫它们属性文件,而是叫其他的东西。只要躲开背后的鞭子…@joerihendricks-meh,别问我:p