最有效的方法来检查Windows中的Java文件是否为空
发布时间:2020-12-13 21:13:33 所属栏目:Windows 来源:网络整理
导读:我试图检查一个日志文件是否为空(意味着没有错误),在Java中,在Windows上。到目前为止,我已经尝试使用2种方法。 方法1(失败) FileInputStream fis = new FileInputStream(new File(sLogFilename)); int iByteCount = fis.read(); if (iByteCount == -1) Sy
我试图检查一个日志文件是否为空(意味着没有错误),在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"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 【旧文章搬运】Windows句柄表分配算法分析(二)
- macos – CGWindowListCopyWindowInfo,kCGWindowLayer和窗口
- Windows资源管理器 – 将文件列表从Windows资源管理器复制为
- windows-server-2003 – 从IIS 6升级到8.5:增加了并发请求
- windows – 如何通过HTTP从Internet检索文件?
- windows-server-2008 – Windows 7 x64 Ultimate上的IIS 7.
- 如何在Windows登录提示出现之前运行批处理脚本?
- 基于WIX的安装程序如何为32位和64位Windows操作系统进行COM
- 如何在Windows中为Visual Studio项目创建Debian安装包?
- windows – 在Visual Studio 2010 Express中创建“.rc”文件