Files
Theory and problem statement
Machine translation, not yet reviewed by a native speaker.
The java.io.File class is a path to a file or a folder, not its contents. There may be no file at that path at all:
import java.io.File;
File f = new File("notes.txt");
f.exists() // whether it exists
f.getName() // "notes.txt"
f.getAbsolutePath() // the full path
f.length() // the size in bytes (0 if it doesn't exist)
f.isFile(), f.isDirectory()
f.canRead(), f.canWrite()
File dir = new File("saves");
dir.mkdir(); // create a folder (mkdirs — with all the parent folders)
new File(dir, "slot1.txt") // a path inside the folder
The modern alternative is java.nio.file.Path and the Files class (Path.of("notes.txt"), Files.exists(p)):
more convenient, and with clear exceptions. The course uses both.
Relative paths start from the current folder, where the program was started.
Task. For each name from the input, print <name>: exists or <name>: missing. Before that, the program creates
the folder saves and the file config.txt with the text volume=80 — at the end, print the size of config.txt in bytes and
whether saves is a folder (true/false).
Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.