英文:
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<String> 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论