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

huangapple 未分类评论41阅读模式
英文:

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

问题

Sure, here is the translated content:

在我的项目中有许多 *.properties 文件,每个文件中都有大量的键值对。

任务是找到所有以 _format_ 开头的键,后面跟着任意单词(我们可以自由地假设该单词不包含任何特殊字符或空白字符),并且该键的值不等于 null(不区分大小写)。

使用一个示例:

_format_Date1=null
_format_Date2=null
_format_endTerm=null
_isUpdatable_somethingA=false
_isUpdatable_somethingB=false
_format_begingTerm=null
_format_countOfMoney=\#0.00#\ -- 必须使用这种模式识别的行
_javaType_name1=String
_javaType_name2=String

我们还可以假设所有的键值对都会被加载到一个集合中,并逐行读取。

任何帮助将不胜感激...

英文:

We have many *.properties files in my project, and in each, we have lots and lots of key=value pairs.

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).

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.

Any help would be appreciated...

答案1

得分: -2

只需迭代文件,逐行读取并进行逐行筛选。

我想类似这样的代码可以实现:

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("文件 " + path.toString() + " 包含匹配行 " + s));
                  } catch (final IOException e) {
                      e.printStackTrace();
                  }
              });
英文:

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();
		              }
	              }
	     );

huangapple
  • 本文由 发表于 2020年4月10日 17:16:57
  • 转载请务必保留本文链接:https://java.coder-hub.com/61137276.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定