简体   繁体   English

使用 Java 识别 *.properties 文件中的键=值对

[英]Identifying key=value pairs in *.properties files using Java

We have many *.properties files in my project, and in each, we have lots and lots of key=value pairs.我的项目中有很多 *.properties 文件,在每个文件中,我们都有很多很多 key=value 对。

The task is to find all keys that start with _format_ followed by any word (we can freely assume that that word will not contain any special characters nor whitespace characters) AND the value of that key IS NOT EQUAL to null (case-insensitive).任务是找到所有以_format_后跟任何单词的键(我们可以自由假设该单词不包含任何特殊字符或空白字符)并且该键的值不等于 null(不区分大小写)。

Using an example:使用示例:

_format_Date1=null
_format_Date2=null
_format_endTerm=null
_isUpdatable_somethingA=false
_isUpdatable_somethingB=false
_format_begingTerm=null
_format_countOfMoney=\#0.00#\ -- The kind of row that must be identified with the pattern
_javaType_name1=String
_javaType_name2=String

We can also assume all of the key=values pairs will loaded up in a collection and read line by line.我们还可以假设所有 key=values 对都将加载到一个集合中并逐行读取。

Any help would be appreciated...任何帮助,将不胜感激...

Just iterate over the files, read them in and filter line by line.只需遍历文件,将它们读入并逐行过滤。

I suppose something like this could do it:我想这样的事情可以做到:

    Files.walk(Paths.get("path"))
         .filter(Files::isRegularFile)
         .filter(path -> path.endsWith(".properties"))
         .forEach(path -> {
                      try {
                          Files.lines(path)
                               .filter(s -> s.startsWith("_format_"))
                               .filter(s -> !s.split("=")[1].toLowerCase().equals("null"))
                               .forEach(s -> System.out.println("File " + path.toString() + " has amtching line " + s));
                      } catch (final IOException e) {
                          e.printStackTrace();
                      }
                  }
         );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM