How to check if a file exists in Java? (NIO and legacy ways)
Hello everyone! I'm a beginner Java developer and I've run into a problem. I'm writing a small application that reads a configuration file `config.properties`. But before reading it, I need to make sure the file actually exists on disk, otherwise a `FileNotFoundException` will be thrown.
I tried using `File.exists()`, but online sources say this method is outdated and doesn't always work correctly (for example, if the file is a symbolic link). I've also heard about the `java.nio.file` package, but I don't fully understand how to use it.
Here's my current code that the linter doesn't like:
```java
import java.io.File;
public class ConfigLoader {
public static void main(String[] args) {
File configFile = new File("/etc/myapp/config.properties");
if (configFile.exists()) {
No answers yet.
Log in to leave an answer
Blogs