Checking if a File is Empty in Java
Hello. In this tutorial, we will take a look at the different approaches to Checking if a File is Empty in Java or not.
1. Introduction
In Java, you can check whether a file is empty or not using several different approaches. One common approach is to check the file’s size to determine if it contains any data.
2. Checking if a File is Empty in Java
Let’s take a closer look at each of the different concepts.
2.1 Using File.length() Method
In this example, the isFileEmpty() method takes the filePath as a parameter and returns a boolean value indicating whether the file is empty or not. Here’s a breakdown of how the method works:
- It creates a
Fileobject using the providedfilePath. - It checks if the file exists using the
exists()method of theFileclass. If the file does not exist, it prints a message and returnsfalse, indicating that the file is not empty. - If the file exists, it uses the
length()method of theFileclass to get the size of the file. If the length is 0, it returnstrue, indicating that the file is empty. Otherwise, it returnsfalse, indicating that the file is not empty.
In the main() method, you can call the isFileEmpty() method with the desired file path. It will return a boolean value indicating whether the file is empty or not, which can be used to display the appropriate message.
Example 1
package org.jcg;
import java.io.File;
public class FileEmptyCheck {
public static void main(String[] args) {
String filePath = "path/to/file.txt";
boolean isEmpty = isFileEmpty(filePath);
if (isEmpty) {
System.out.println("The file is empty.");
} else {
System.out.println("The file is not empty.");
}
}
/**
* Checks if a file is empty.
*
* @param filePath the path of the file to check
* @return true if the file is empty, false otherwise
*/
public static boolean isFileEmpty(String filePath) {
// Create a File object with the specified file path
File file = new File(filePath);
// Check if the file exists
if (!file.exists()) {
System.out.println("File does not exist.");
return false; // File does not exist, not considered empty
}
// Check if the file length is 0
if (file.length() == 0) {
return true; // File is empty
} else {
return false; // File is not empty
}
}
}
2.1.1 Output of the above program
The output of the code will depend on the file you provide the `filePath` variable. Here are the possible outputs:
- If the file is empty:
The file is empty.
- If the file is not empty:
The file is not empty.
- If the file does not exist:
File does not exist.
Make sure to replace “path/to/file.txt” in the code with the actual path and filename of the file you want to check.
2.2 Using FileChannel.size() Method
In this example, the isFileEmpty() method takes the filePath as a parameter and returns a boolean value indicating whether the file is empty or not. Here’s a breakdown of how the method works:
- It creates a
Fileobject using the providedfilePath. - It checks if the file exists using the
exists()method of theFileclass. If the file does not exist, it prints a message and returnsfalse, indicating that the file is not empty. - It creates a
FileInputStreamto read the file. - Inside a
tryblock with resources, it obtains theFileChannelassociated with theFileInputStreamusing thegetChannel()method. - It uses the
size()method ofFileChannelto get the size of the file. - It checks if the file size is 0. If the size is 0, it returns
true, indicating that the file is empty. Otherwise, it returnsfalse, indicating that the file is not empty. - If any
IOExceptionoccurs during the process, it catches the exception, prints an error message, and returnsfalse, indicating that the file’s emptiness couldn’t be determined due to an error.
In the main() method, you can call the isFileEmpty() method with the desired file path. It will return a boolean value indicating whether the file is empty or not, which can be used to display the appropriate message.
Example 2
package org.jcg;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class FileEmptyCheck_2 {
public static void main(String[] args) {
String filePath = "path/to/file.txt";
boolean isEmpty = isFileEmpty(filePath);
if (isEmpty) {
System.out.println("The file is empty.");
} else {
System.out.println("The file is not empty.");
}
}
/**
* Checks if a file is empty.
*
* @param filePath the path of the file to check
* @return true if the file is empty, false otherwise
*/
public static boolean isFileEmpty(String filePath) {
// Create a File object with the specified file path
File file = new File(filePath);
// Check if the file exists
if (!file.exists()) {
System.out.println("File does not exist.");
return false; // File does not exist, not considered empty
}
// Create a FileInputStream to read the file
try (FileInputStream fis = new FileInputStream(file)) {
// Get the file channel associated with the FileInputStream
FileChannel fileChannel = fis.getChannel();
// Get the size of the file using the size() method of FileChannel
long fileSize = fileChannel.size();
// Check if the file size is 0
if (fileSize == 0) {
return true; // File is empty
} else {
return false; // File is not empty
}
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
return false; // Unable to determine if the file is empty due to an error
}
}
}
2.2.1 Output of the above program
The output of the code will depend on the file you provide the `filePath` variable. Here are the possible outputs:
- If the file is empty:
The file is empty.
- If the file is not empty:
The file is not empty.
- If the file does not exist:
File does not exist.
Make sure to replace “path/to/file.txt” in the code with the actual path and filename of the file you want to check.
2.3 Using FileReader and BufferedReader
In this example, the isFileEmpty() method takes the filePath as a parameter and returns a boolean value indicating whether the file is empty or not. Here’s a breakdown of how the method works:
- It creates a
Fileobject using the providedfilePath. - It checks if the file exists using the
exists()method of theFileclass. If the file does not exist, it prints a message and returnsfalse, indicating that the file is not empty. - It creates a
FileReaderandBufferedReaderto read the file. - Inside a
tryblock with resources, it checks if the first line of the file, obtained using thereadLine()method ofBufferedReader, isnull. If it isnull, it means the file is empty, and it returnstrue. Otherwise, it returnsfalseindicating that the file is not empty. - If any
IOExceptionoccurs during the process, it catches the exception, prints an error message, and returnsfalse, indicating that the file’s emptiness couldn’t be determined due to an error.
In the main() method, you can call the isFileEmpty() method with the desired file path. It will return a boolean value indicating whether the file is empty or not, which can be used to display the appropriate message.
Example 3
package org.jcg;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileEmptyCheck_3 {
public static void main(String[] args) {
String filePath = "path/to/file.txt";
boolean isEmpty = isFileEmpty(filePath);
if (isEmpty) {
System.out.println("The file is empty.");
} else {
System.out.println("The file is not empty.");
}
}
/**
* Checks if a file is empty.
*
* @param filePath the path of the file to check
* @return true if the file is empty, false otherwise
*/
public static boolean isFileEmpty(String filePath) {
// Create a File object with the specified file path
File file = new File(filePath);
// Check if the file exists
if (!file.exists()) {
System.out.println("File does not exist.");
return false; // File does not exist, not considered empty
}
// Create FileReader and BufferedReader to read the file
try (FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader)) {
// Check if the first line of the file is null
if (bufferedReader.readLine() == null) {
return true; // File is empty
} else {
return false; // File is not empty
}
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
return false; // Unable to determine if the file is empty due to an error
}
}
}
2.3.1 Output of the above program
The output of the code will depend on the file you provide the `filePath` variable. Here are the possible outputs:
- If the file is empty:
The file is empty.
- If the file is not empty:
The file is not empty.
- If the file does not exist:
File does not exist.
Make sure to replace “path/to/file.txt” in the code with the actual path and filename of the file you want to check.
2.4 Using FileInputStream and BufferedInputStream
In this example, the isFileEmpty() method takes the filePath as a parameter and returns a boolean value indicating whether the file is empty or not. Here’s a breakdown of how the method works:
- It creates a
Fileobject using the providedfilePath. - It checks if the file exists using the
exists()method of theFileclass. If the file does not exist, it prints a message and returnsfalse, indicating that the file is not empty. - It creates a
FileInputStreamandBufferedInputStreamto read the file. - Inside a
tryblock with resources, it checks if the number of bytes available to read from theBufferedInputStream, obtained using theavailable()method, is 0. If it is 0, it means the file is empty, and it returnstrue. Otherwise, it returnsfalse, indicating that the file is not empty. - If any
IOExceptionoccurs during the process, it catches the exception, prints an error message, and returnsfalse, indicating that the file’s emptiness couldn’t be determined due to an error.
In the main() method, you can call the isFileEmpty() method with the desired file path. It will return a boolean value indicating whether the file is empty or not, which can be used to display the appropriate message.
Example 4
package org.jcg;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileEmptyCheck_4 {
public static void main(String[] args) {
String filePath = "path/to/file.txt";
boolean isEmpty = isFileEmpty(filePath);
if (isEmpty) {
System.out.println("The file is empty.");
} else {
System.out.println("The file is not empty.");
}
}
/**
* Checks if a file is empty.
*
* @param filePath the path of the file to check
* @return true if the file is empty, false otherwise
*/
public static boolean isFileEmpty(String filePath) {
// Create a File object with the specified file path
File file = new File(filePath);
// Check if the file exists
if (!file.exists()) {
System.out.println("File does not exist.");
return false; // File does not exist, not considered empty
}
// Create FileInputStream and BufferedInputStream to read the file
try (FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
// Check if the file size is 0
if (bufferedInputStream.available() == 0) {
return true; // File is empty
} else {
return false; // File is not empty
}
} catch (IOException e) {
System.out.println("An error occurred: " + e.getMessage());
return false; // Unable to determine if the file is empty due to an error
}
}
}
2.4.1 Output of the above program
The output of the code will depend on the file you provide the `filePath` variable. Here are the possible outputs:
- If the file is empty:
The file is empty.
- If the file is not empty:
The file is not empty.
- If the file does not exist:
File does not exist.
Make sure to replace “path/to/file.txt” in the code with the actual path and filename of the file you want to check.
3. Conclusion
3.1 Using File.length() Method
The first approach to check if a file is empty is by using the length() method provided by the File class. This method returns the size of the file in bytes. By checking if the length of the file is 0, we can determine if the file is empty or not. This approach is straightforward and efficient as it directly gives us the size of the file without reading its contents. However, it requires the file to exist on the file system.
3.2 Using FileChannel.size() Method
The second approach involves using the size() method provided by the FileChannel class. This method returns the size of the file in bytes, similar to the length() method. By obtaining the FileChannel associated with the file and then calling the size() method, we can determine if the file is empty by checking if the size is 0. This approach also provides a direct way of obtaining the file size without reading its contents, but it requires the file to exist on the file system.
3.3 Using FileReader and BufferedReader
The third approach involves using the FileReader and BufferedReader classes to read the file and check if its contents are empty. By reading the first line of the file using the readLine() method of the BufferedReader class and checking if it is null, we can determine if the file is empty. This approach reads the file contents, so it may not be as efficient as the previous approaches for large files. However, it does not require the file to exist on the file system.
3.4 Using FileInputStream and BufferedInputStream
The fourth approach involves using the FileInputStream and BufferedInputStream classes to read the file and check if its contents are empty. By checking the number of bytes available to read from the BufferedInputStream using the available() method, we can determine if the file is empty by checking if the number of bytes is 0. This approach reads the file contents and provides a way to determine if the file is empty without relying on the file’s existence in the file system.
In conclusion, there are multiple approaches to checking if a file is empty in Java. The choice of approach depends on factors such as the requirement of file existence, efficiency, and whether the file contents need to be read. The length() and size() methods provide direct access to the file size, while the readLine() and available() methods read the file contents. Consider the specific requirements of your application to select the most suitable approach for determining if a file is empty or not.
This concludes our tutorial, and I trust that the article provided you with the information you sought. I wish you happy learning and encourage you to share your newfound knowledge with others! You can download the source code from the Downloads section.
4. Download the Files
This was a tutorial to understand different approaches to check if a file is empty in Java or not
You can download the files of this example here: Checking if a File is Empty in Java

