Home > Java > JavaBase > body text

Is the file empty in java?

Release: 2019-11-21 13:27:16
Original
2402 people have browsed it

Is the file empty in java?

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

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

如果File表示一个具体文件,则length方法返回这个文件的字节大小;

如果File表示一个目录,则length方法的返回值不确定;

如果File表示的目录或文件不存在,则length方法的返回值为0L。

更多java知识请关注java基础教程

The above is the detailed content of Is the file empty in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template