+91 81900 98289

Java File Handling

java.nio.file.Path is a location. Path.of("notes", "today.txt") builds it from parts so you do not hard-code one operating system's slash. Files is the class with read, write, exists, and delete.

Checked exceptions such as IOException must be caught or declared with throws. A short main can declare throws Exception while you are learning. A real program should catch it and say which file failed.

Example

getFileName returns the last part of the path, not the notes folder.

import java.nio.file.Path; public class Locate { public static void main(String[] args) { Path path = Path.of("notes", "today.txt"); System.out.println(path.getFileName()); } }
This lesson only builds a path. It does not create the file. The read and write lessons do the disk work.