简体   繁体   English

使用 BufferReader '\\n' 读取后不会被接受为换行符,如何解决?

[英]After reading with BufferReader '\n' won't be accepted as a new line char, how to solve this?

I have a large text file I want to format.我有一个要格式化的大文本文件。 Say the input file is called inputFile and output file is called outputFile .假设输入文件名为inputFile ,输出文件名为outputFile

This is my code for using BufferedReader and BufferedWriter Here is my code这是我使用BufferedReaderBufferedWriter代码 这是我的代码

 public static void readAndWrite(String fileNameToRead, String fileNameToWrite) {
        try{
            BufferedReader fr = new BufferedReader(
                    new FileReader(String.format("%s.txt", fileNameToRead)));
            BufferedWriter out = new BufferedWriter(
                    new FileWriter(String.format("%s.txt", fileNameToWrite), true));
            String currentTmp = "";
            String tmp = "";

            String test = "work \nwork";
            out.append(test);


            while((tmp = fr.readLine()) != null) {
                tmp = tmp.trim();
                if(tmp.isEmpty()) {
                    currentTmp = currentTmp.trim();
                    out.append(currentTmp);
                    out.newLine();
                    out.newLine();
                    currentTmp = "";
                } else {
                    currentTmp = currentTmp.concat(" ").concat(tmp);
                }
            }
            if(!currentTmp.equals("")) {
                out.write(currentTmp);
            }
            fr.close();
            out.close();
        } catch (IOException e) {
            System.out.println("exception occoured" + e);
        }

    }

    public static void main(String[] args) {
        String readFile = "inPutFile";
        String writeFile = "outPutFile";
        readAndWrite(readFile, writeFile);
    }

The problem is that the test string inside the code which have '\\n' can we converted to a new line with BufferedWriter .问题是代码中包含 '\\n' 的test字符串我们可以用BufferedWriter转换为新行。 But if I put the same string in the text file it would not perform the same.但是,如果我将相同的字符串放在文本文件中,它的表现将不一样。

In a more easy way to see is that I want my input file have this更简单的方式是我希望我的输入文件有这个

work\n
work

and output as并输出为

work 
work

I am using mac, so the separator should be '\\n'我使用的是 mac,所以分隔符应该是 '\\n'

work\n 

if you see the "\\n" in your file, it is not a new line character.如果您在文件中看到“\\n”,则它不是换行符。 It is just two characters.它只是两个字符。

The trim() method will not remove those characters. trim()方法不会删除这些字符。

Instead you might have something like:相反,您可能有类似的东西:

if (tmp.endsWith("\n")
    tmp = tmp.substring(0, tmp.length() - 2);

I am using mac, so the separator should be '\\n'我使用的是 mac,所以分隔符应该是 '\\n'

You should use the newline character for the platform.您应该为平台使用换行符。 So when writing to your file the code should be:因此,在写入文件时,代码应该是:

} else {
    currentTmp = currentTmp.concat(" ").concat(tmp);
    out.append( currentTmp );
    out.newLine();
}

The newline() method will use the appropriate new line String for the platform. newline() 方法将为平台使用适当的新行字符串。

Edit:编辑:

You need to understand what an escape character is in Java.您需要了解 Java 中的转义字符是什么。 When you use:当您使用:

String text = "test\n"

and write the string to a file, only 5 characters are written to the file, not 6. The "\\n" is an escape sequence which will cause the ascii value for the new line character to be added to the file.并将字符串写入文件,只写入 5 个字符,而不是 6 个。“\\n”是一个转义序列,它将导致将换行符的 ascii 值添加到文件中。 This character is not displayable so you can't see it in the file.此字符不可显示,因此您无法在文件中看到它。

After @camickr answer, I think I realized the problem.在@camickr 回答之后,我想我意识到了这个问题。 Some how if I have a text in the file like this如果我在文件中有这样的文本,该怎么办

work \nwork

The \\n won't be treated as a single char ('\\n'), rather it has been treated as two chars. \\n不会被视为单个字符 ('\\n'),而是被视为两个字符。 I think thats why when the BufferWriter writes the input string it won't treat it as a new line.我认为这就是为什么当BufferWriter写入输入字符串时,它不会将其视为新行。

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

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