Home  >  Article  >  Java  >  How to determine whether a file is empty in java

How to determine whether a file is empty in java

王林
王林Original
2019-11-21 13:28:528064browse

How to determine whether a file is empty in java

在File类中并没有提供判断文件是否为空的方法,但可以借助length()方法的返回值进行判断。

length()方法定义为:

public long length()

如果文件不存在或文件为空时,length()方法返回0。

示例代码:

	File file = new File("D:\\test.txt");
	if(file.exists() && file.length)() == 0) {
		System.out.println("文件为空!");
	}

推荐教程:java快速入门

The above is the detailed content of How to determine whether a file is empty in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn