search
HomeJavaJavaBaseIs the file empty in java?

Is the file empty in java?

Nov 21, 2019 pm 01:27 PM
java

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("文件为空!");  
}

如果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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor