Java:查找文件的最后一行是否为空 [英] Java: Find if the last line of a file is empty

查看:279
本文介绍了Java:查找文件的最后一行是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于学校作业的问题,我需要用Java来完成。
我需要从文件加载数据并检查这些文件中的错误。

我用 bufferedReader 读取这个文件,直到文件结尾为止:它忽略最后一行如果该行是空的。

我知道如何检查空行,但是最后一行没有给出使用 readLine() bufferedReader 的函数。

我知道最后一行是否为空是非常重要的。如果空行不存在,则应该给出错误。

长话短说,我需要一种方法来区分以下情况(其中 CRLF 是行结束):

情况1(正确):

  line x CRLF 
line y CRLF

情况2(错误):

  line x CRLF 
line y

这两种情况都会在行y之后的 readline()上返回null 。


我正在计算文件的路线,所以如果我有一个行计数器(注意:计数器必须计算这个空行,所有的我发现没有计算它们)

这些文件也包含空行,如果这对我所需要的代码有任何影响(这些行是正确检测到的,因为它们应该是因为 EOF 不在这些行上)



请注意,程序可以使用或不使用最后一行,如果它不在那里,这个任务纯粹是告诉我要给出一个错误。

解决方案

如果您想确定最后一行是否有CRLF,您可以从结尾读取。
$ b

  public static boolean lastLineisCRLF(String filename){
RandomAccessFile raf = null;
尝试{
raf = new RandomAccessFile(filename,r);
long pos = raf.length() - 2;
if(pos <0)返回false; //太短
raf.seek(pos);
return raf.read()=='\r'&& raf.read()=='\\\
';
} catch(IOException e){
return false;
} finally {
if(raf!= null)try {
raf.close();
} catch(IOException被忽略){
}
}
}


I have a question about a school assignment I need to do it in Java. I need to load data from a file and check for errors in these files.

I read the file with a bufferedReader which works perfectly until the end of the file: it ignores the last line if that line is empty.

I know how to check for empty lines, but the last line simply doesn't give any result using the readLine() function of bufferedReader.

It's important that I know if the last line is empty as it must be. If the empty line doesn't exist, it should give an error.

So long story short, I need a way to tell the difference between the following situations (where CRLF is the end of the line):

Situation 1 (correct):

line x CRLF
line y CRLF

Situation 2 (wrong):

line x CRLF
line y

Both of these situations will return a null on readline() after line y.

I am counting the lines of the file on the way, so if I have a line counter (Note: that counter must count that empty line too, all the ones I found did not count them)

The files contain empty lines throughout too, if that should make any difference for the code I need (these lines are properly detected as they should be as the EOF isn't on these lines)

Note that the program works with or without that last line, it's purely that the assignment tells me to give an error if it's not there.

解决方案

If you want to determine if the last line has a CRLF you can read from the end.

public static boolean lastLineisCRLF(String filename) {
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(filename, "r");
        long pos = raf.length() - 2;
        if (pos < 0) return false; // too short
        raf.seek(pos);
        return raf.read() == '\r' && raf.read() == '\n';
    } catch (IOException e) {
        return false;
    } finally {
        if (raf != null) try {
            raf.close();
        } catch (IOException ignored) {
        }
    }
}

这篇关于Java:查找文件的最后一行是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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