Java Working with Files
The Files class also manages files and folders themselves. It builds folders, copies, moves, lists, and deletes, all by path.
Build a folder and copy a file
createDirectories builds the room folder, and copy duplicates the note inside it. The two sizes print equal.
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws Exception {
Files.createDirectories(Path.of("room"));
Files.writeString(Path.of("room/note.txt"), "Hello, room.");
Files.copy(Path.of("room/note.txt"), Path.of("room/note-copy.txt"));
System.out.println(Files.exists(Path.of("room")));
System.out.println(Files.size(Path.of("room/note.txt")));
System.out.println(Files.size(Path.of("room/note-copy.txt")));
}
}Move a file to a new name
The move call renames old.txt to new.txt. The old name prints false and the new name prints true.
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws Exception {
Files.writeString(Path.of("old.txt"), "Moving day.");
Files.move(Path.of("old.txt"), Path.of("new.txt"));
System.out.println(Files.exists(Path.of("old.txt")));
System.out.println(Files.exists(Path.of("new.txt")));
}
}List a folder in sorted order
The files are written in pear, apple, mango order. The names are collected and sorted before printing, so apple comes first.
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws Exception {
Files.createDirectories(Path.of("shelf"));
Files.writeString(Path.of("shelf/pear.txt"), "Pear");
Files.writeString(Path.of("shelf/apple.txt"), "Apple");
Files.writeString(Path.of("shelf/mango.txt"), "Mango");
List<String> names = new ArrayList<String>();
try (Stream<Path> stream = Files.list(Path.of("shelf"))) {
stream.forEach(path -> names.add(path.getFileName().toString()));
}
Collections.sort(names);
for (String name : names) {
System.out.println(name);
}
}
}Delete a file
The file is written, then deleted. The exists check after the delete prints false.
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) throws Exception {
Files.writeString(Path.of("gone.txt"), "Soon gone.");
Files.delete(Path.of("gone.txt"));
System.out.println(Files.exists(Path.of("gone.txt")));
}
}Example
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws Exception {
Files.createDirectories(Path.of("desk"));
Files.writeString(Path.of("desk/b.txt"), "Berry");
Files.writeString(Path.of("desk/a.txt"), "Apple");
List<String> names = new ArrayList<String>();
try (Stream<Path> stream = Files.list(Path.of("desk"))) {
stream.forEach(path -> names.add(path.getFileName().toString()));
}
Collections.sort(names);
for (String name : names) {
System.out.println(name);
}
Files.copy(Path.of("desk/a.txt"), Path.of("desk/a-copy.txt"));
System.out.println(Files.size(Path.of("desk/a.txt")));
System.out.println(Files.size(Path.of("desk/a-copy.txt")));
Files.delete(Path.of("desk/a-copy.txt"));
System.out.println(Files.exists(Path.of("desk/a-copy.txt")));
}
}Key points
- Files.createDirectories builds a folder path in one call.
- Files.copy duplicates a file, and both sizes print equal after the copy.
- Files.move renames a file, so the old name stops existing.
- Files.list walks a folder, and its names print sorted in this lesson.
- Files.delete removes a file, and exists prints false after the removal.
- Files.exists answers true or false for any path.
In this example, the desk folder is built and filled, then its names print in sorted order. The copy of a.txt matches its size, and after the delete the copy no longer exists.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
Which method builds a folder path in one call?