This post will discuss how to check whether a file is empty in Kotlin.

1. Using BufferedReader.readLine() function

The readLine() function of the BufferedReader class returns null if the end of the stream is reached without reading any characters. We can use this to check whether a file is empty, as shown below:

Download Code

2. Using File.length() function

Another alternative is to construct the File object and call its length() function to get the file’s length. The file is empty if and only if its length is 0.

Download Code

 
This approach is not recommended since the length() function returns 0 even when the file does not exist, is a directory, or an I/O error occurs.

That’s all about checking whether a file is empty in Kotlin.