This post will discuss how to check if a file is empty in Java.

1. Using BufferedReader.readLine() method

A simple solution is to get a character-input stream from BufferedReader using the readLine() method. It returns null if the end of the stream has been reached without reading any characters. This can be used to check if a file is empty, as demonstrated below:

Download Code

2. Using Apache Commons IO

With Apache Commons IO, you can use the FileUtils.readFileToString() method that reads the content of any file into a string. Then you can simply check if the string is empty or not. If the string is empty, the file must be empty, otherwise not.

Download Code

3. Using File.length() method

Another option is to create the File instance and call its length() method to get the length of the file. If the file is empty, its length must be 0. But the opposite might not be true, since the length() method returns 0 if the file does not exist, is a directory, or any other I/O exception happens.

Download Code

That’s all about checking if a file is empty in Java.