Home >Java >JavaBase >Java determines whether the file is empty

Java determines whether the file is empty

尚
Original
2019-11-19 15:06:024176browse

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。

Java determines whether the file is empty

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

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

Java文件类以抽象的方式代表文件名和目录路径名。File类主要用于文件和目录的创建、文件的查找和文件的删除等。File对象代表磁盘中实际存在的文件和目录。

length() 方法用于返回字符串的长度。长度等于字符串中 16 位 Unicode 代码单元的数量。

语法:

public int length()

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

The above is the detailed content of Java determines whether the file is empty. 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