繁体   English   中英

如何在java中读取文件时跳过回车符作为换行符

[英]How to skip carriage return as line breaker while reading a file in java

我正在使用java中的BufferedReader.readLine()读取文本文件。 我的文本文件是使用隐藏换行符创建的。 我的问题是我需要跳过回车符( \\r )作为换行符,只需要考虑换行符( \\n )作为换行符。

我怎样才能做到这一点?

你必须编写自己的readLine BufferedReader.readLine将所有\\ r,\\ n和\\ r \\ n视为换行符,您无法更改它。 在你定义自己的换行符的地方做一个帮手。

编辑:看起来像这样

String readLineIgnoreCR(BufferedReader reader)
{
    int c = reader.read();
    String line = "";
    while(c >= 0)
    {
        if((char) c == '\r')
            continue;
        else if((char) c == '\n')
            return line;
        line += (char) c;

    }
}

是正确的:

    String readLineIgnoreCR(BufferedReader reader) {
        int c = 0;
        String line = "";
        while(c >= 0) {
            c = reader.read();
            if((char) c == '\r')
                continue;
            else if((char) c == '\n')
                return line;
            line += (char) c;
        }
        return line;
    }

暂无
暂无

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

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