Check if a file exists in Kotlin
This post will discuss how to check if a file exists in Kotlin. The solution should return true if the file exists, and false if the file doesn’t exist or the file’s status is unknown.
1. Using File class
You can use the File.exists() function to determine if the specified file exists or not. It returns true if the file exists; false otherwise. Since a file can be a directory, the function returns true when the specified path points to a directory. To handle this, additionally check for a directory using the File.isDirectory() function. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.File fun isFileExists(file: File): Boolean { return file.exists() && !file.isDirectory } fun main() { val filePath = "/sys/data.log" val file = File(filePath) if (isFileExists(file)) { println("File exists!!") } else { println("File doesn't exist or program doesn't have access to it") } } |
You can avoid an additional check for a directory using the File.isFile() function. It tests whether the specified path points to a regular file, and not a directory.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.io.File fun isFileExists(file: File): Boolean { return file.isFile } fun main() { val filePath = "/sys/data.log" val file = File(filePath) if (isFileExists(file)) { println("File exists!!") } else { println("File doesn't exist or program doesn't have access to it") } } |
2. Using Files class
To check for a file’s existence, you can use Files.exists() and Files.notExists() function along with the Files.isDirectory() function. The exists() function returns true if the file exists (or if the specified path points to a directory), whereas the notExists() function returns true when it does not exist.
Note that if both exists() and notExists() return false, the existence of the file cannot be verified. This can happen when the program does not have access to the file.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.nio.file.Files import java.nio.file.Paths fun main() { val filePath = "/sys/data.log" val path = Paths.get(filePath) when { Files.isDirectory(path) -> { println("File is a Directory") } Files.exists(path) -> { println("File exists!!") } Files.notExists(path) -> { println("File doesn't exist!!") } else -> { println("Program doesn't have access to the file!!") } } } |
Alternatively to check if a path is a regular file (and not a directory), use the Files.isRegularFile() function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.nio.file.Files import java.nio.file.Paths fun main() { val filePath = "/sys/data.log" val path = Paths.get(filePath) val exists = Files.isRegularFile(path) if (exists) { println("File exists!!") } else { println("File doesn't exist!!") } } |
That’s all about checking if a file exists in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)