Share this page 

Check if a file existsTag(s): IO


The classic way
import java.io.*;

public class FileTest {
  public static void main(String args[]) {
    File f = new File(args[0]);
    System.out.println
      (f + (f.exists()? " is found " : " is missing "));
  }
}
The NIO way
import java.nio.file.Paths;
import java.nio.file.Files;

..

String file = "C:\\file.txt";
Path path = Paths.get(location);

System.out.println(Files.exists(path));
Some interesting facts about Files.Exists