如何使用流读取文件时保留换行符 - java 8 [英] How to preserve newlines while reading a file using stream - java 8

查看:1219
本文介绍了如何使用流读取文件时保留换行符 - java 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

      try (Stream<String> lines = Files.lines(targetFile)) {  
     List<String> replacedContent = lines.map(line ->  
                                       StringUtils.replaceEach(line,keys, values))
                                       .parallel()
                                       .collect(Collectors.toList());
    Files.write(targetFile, replacedContent);
}

我正在尝试替换文件每行中的多个文本模式。但我观察到\\\\ n(字节等效10和13)被替换为\ r(仅为10)并且我的比较测试失败了。

I'm trying to replace multiple text patterns in each line of the file. But I'm observing that "\r\n"(byte equivalent 10 and 13) is being replaced with just "\r"(just 10) and my comparison tests are failing.

我想保留新行,因为它们在输入文件中,并且不希望java触及它们。任何人都可以建议是否有办法这样做而不必使用单独的默认替换\\\ n。

I want to preserve the newlines as they are in the input file and don't want java to touch them. Could anyone suggest if there is a way to do this without having to use a separate default replacement for "\r\n".

推荐答案

问题是 Files.lines()是在顶部 BufferedReader.readLine(),它读取一行直到行终止符并将其抛弃。然后,当您使用类似 Files.write()的行编写行时,这将在每行之后提供系统特定的行终止符,这可能与行终止符不同。读入。

The problem is that Files.lines() is implemented on top of BufferedReader.readLine(), which reads a line up until the line terminator and throws it away. Then, when you write the lines with something like Files.write(), this supplies the system-specific line terminator after each line, which might differ from the line terminator that was read in.

如果你真的想要完全保留行终止符,即使它们是不同行终止符的混合,你也可以使用正则表达式和扫描器为此。

If you really want to preserve the line terminators exactly as they are, even if they're a mixture of different line terminators, you could use a regex and Scanner for that.

首先定义一个匹配包含有效行终止符或EOF的行的模式:

First define a pattern that matches a line including the valid line terminators or EOF:

Pattern pat = Pattern.compile(".*\\R|.+\\z");

\\\\ 是一个特殊的linebreak matcher,它匹配通常的行终止符以及一些我从未听说过的Unicode行终止符。 :-)你可以使用类似(\\\\\\ | \\r | \\ n)的东西,如果你只想要通常 CRLF CR LF 终止符。

The \\R is a special linebreak matcher that matches the usual line terminators plus a few Unicode line terminators that I've never heard of. :-) You could use something like (\\r\\n|\\r|\\n) if you want just the usual CRLF, CR, or LF terminators.

您必须包含。+ \\\\ 以匹配文件中没有行终止符的潜在最后行。确保正则表达式始终匹配至少一个字符,以便在扫描程序到达文件末尾时找不到匹配项。

You have to include .+\\z in order to match a potential last "line" in the file that doesn't have a line terminator. Make sure the regex always matches at least one character so that no match will be found when the Scanner reaches the end of the file.

然后,使用<$读取行c $ c>扫描仪直到它返回 null

Then, read lines using a Scanner until it returns null:

try (Scanner in = new Scanner(Paths.get(INFILE), "UTF-8")) {
    String line;
    while ((line = in.findWithinHorizon(pat, 0)) != null) {
        // Process the line, then write the output using something like
        // FileWriter.write(String) that doesn't add another line terminator.
    }
}

这篇关于如何使用流读取文件时保留换行符 - java 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆