从文本文件中使用Java删除带有不需要字符的行。

huangapple go评论79阅读模式
英文:

Remove the line with unwanted characters from text file using java

问题

我想要过滤掉文本文件中包含许多未正确识别的单词的行。这些单词将通过特殊符号来识别,例如 @/(&#%-"。

例如,给定以下带有行的文件:

I like to eat.
I &^#@dj dsaasd.
I like to drink.

输出应该如下所示:

I like to eat.
I like to drink.

它将删除包含特殊符号的行。

英文:

I want to filter out the line in a text file which is having many words that were not recognize properly. The word will recognize with special symbols such as @/(&#%-"

For example the file given with lines:

I like to eat.
I &^#@dj dsaasd.
I like to drink.

And the output should be like:

I like to eat.
I like to drink.

It will remove the line with the special symbols.

答案1

得分: 1

读取文件内容作为行,你正在使用Java:

Stream<String> lines = Files.lines(path, encoding);

现在你需要应用你的过滤器:

lines.filter(line -> line.matches(你想要的正则表达式));

然后使用你想要的收集器。

要应用这个解决方案,你需要学习流(Streams)和lambda表达式,并且你使用的是Java 8及更高版本。

英文:

Read file content as lines, you are using java :

Stream&lt;String&gt; lines = Files.lines(path, encoding);

Now you need to apply your filter

lines.filter(line -> line.matches(regex that you want));

Then use the collector that you want

To apply this solution you need to learn Streams and lambda expression and you use java8 and higher.

</details>



huangapple
  • 本文由 发表于 2020年7月21日 22:28:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63016782.html
匿名

发表评论

匿名网友

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

确定