Java Files

File handling lets your program store information that survives after it stops running. Java offers two main toolkits for this: the older java.io package built around the File class, and the newer java.nio.file package built around Path and Files. This lesson introduces both and shows how to inspect files on disk.

Up to now your programs have kept everything in memory, which disappears when the program ends. Files change that. By reading from and writing to files, your program can save user data, load configuration, or process documents. Java has supported files since its earliest versions, and modern Java gives you cleaner tools through the java.nio.file package.

Two ways to work with files

ToolkitKey classesNotes
java.io (classic)File, FileReader, FileWriterSimple, available everywhere, older style
java.nio.file (modern)Path, Paths, FilesCleaner methods, better error info, preferred today

Both approaches work, and you will see both in real codebases. The classic File class from java.io is still common in tutorials, while the Files helper class from java.nio.file offers convenient one-line methods for reading and writing. Learning both means you can read any Java code you come across.

Inspecting a file with the classic File class

import java.io.File;

public class Main {
  public static void main(String[] args) {
    File myFile = new File("notes.txt");

    if (myFile.exists()) {
      System.out.println("Name: " + myFile.getName());
      System.out.println("Path: " + myFile.getAbsolutePath());
      System.out.println("Writable: " + myFile.canWrite());
      System.out.println("Size in bytes: " + myFile.length());
    } else {
      System.out.println("The file does not exist yet.");
    }
  }
}

Creating a File object does not create a file on disk; it just represents a path. The exists() method tells you whether an actual file is there. This is why checking before you act is a good habit.

The modern Path and Files approach

Inspecting a file with java.nio.file

import java.nio.file.Path;
import java.nio.file.Files;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {
    Path path = Path.of("notes.txt");

    if (Files.exists(path)) {
      System.out.println("File name: " + path.getFileName());
      System.out.println("Size: " + Files.size(path) + " bytes");
      System.out.println("Readable: " + Files.isReadable(path));
    } else {
      System.out.println("No such file.");
    }
  }
}
  • A File or Path object only describes a location; it does not create the file.
  • Use exists() to check whether a file is actually present before using it.
  • java.io centres on the File class; java.nio.file centres on Path and the Files helper.
  • Modern code generally prefers java.nio.file for its cleaner methods.
Note: Almost every file operation can throw an IOException because things like missing files or permission problems are outside your program's control. That is why file methods either need a try-catch or a throws clause on the method, as shown above.
Note: Prefer relative paths like "notes.txt" during learning so files stay in your project folder. Absolute paths such as C:/Users/... tie your program to one machine and break when shared.