最有效的方法来检查Windows中的Java文件是否为空

我试图检查一个日志文件是否为空(意味着没有错误),在Java中,在Windows上。到目前为止,我已经尝试使用2种方法。

方法1(失败)

FileInputStream fis = new FileInputStream(new File(sLogFilename));  
int iByteCount = fis.read();  
if (iByteCount == -1)  
    System.out.println("NO ERRORS!");
else
    System.out.println("SOME ERRORS!");

方法2(失败)

File logFile = new File(sLogFilename);
if(logFile.length() == 0)
    System.out.println("NO ERRORS!");
else
    System.out.println("SOME ERRORS!");

现在这两种方法在日志文件为空(没有内容)时失败,但文件大小不为零(2字节)。

检查文件是否为空的最有效和准确的方法是什么?我要求效率,因为我必须继续检查文件大小数千次。

注意:文件大小只能悬停在几个到10 KB之间!

方法3(失败)

遵循@ Cygnusx1的建议,我也尝试使用FileReader,没有成功。如果有人有兴趣,这是代码段。

Reader reader = new FileReader(sLogFilename);
int readSize = reader.read();
if (readSize == -1)
    System.out.println("NO ERRORS!");
else
    System.out.println("SOME ERRORS!");
检查文件的第一行是否为空:
BufferedReader br = new BufferedReader(new FileReader("path_to_some_file"));     
if (br.readLine() == null) {
    System.out.println("No errors,and file empty");
}

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...