Files and folders
Everything a program keeps in its variables is gone when it ends. Data that has to last, such as contacts, high scores or a year of measurements, is kept in files.
- A file is a piece of data stored under a name: a photo, a song, a document, a program. This module reads text files, which hold plain text in lines. Any text editor can open and change them, and your
.javafiles are text files too. - The end of a file's name, such as
.txt,.csvor.java, is its extension. It tells what kind of data the file holds, and it's part of the name:scoresandscores.txtare two different names. Windows and macOS can hide extensions when they list files, but the extension is still there. - A folder (also called a directory) holds files and other folders. A file's path names the folders on the way to it:
school/biology/cells.txtis the filecells.txtin the folderbiology, which is in the folderschool. Windows writes paths with\instead of/.
Where does the program look?
When a program opens a file by its name alone, such as plants.txt, Java looks for it in the program's working folder (or working directory): the folder the program runs in.
- On your own computer, when you run a project from an editor such as NetBeans, IntelliJ IDEA or VS Code, the working folder is usually the project's main folder, the one that holds the
srcfolder. A file your program reads goes there, next tosrc, not inside it. - On Java Arena there's no project folder of your own. Instead, each challenge that reads files provides them: the task shows each file with its name and contents, and the check puts the files in your program's working folder before it runs, just like files next to a program on a computer. Run with my input gets the same files. Hidden tests give files with other contents, so the program has to really read them.
A first look
This program reads the file plants.txt and prints a line for each plant in it:
basil
mint
rosemaryimport java.nio.file.Paths;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(Paths.get("plants.txt"))) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println("Water the " + line);
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Water the basil
Water the mint
Water the rosemaryThe next steps go through this program part by part. For now, find the file's name in Paths.get("plants.txt"): that's where the program says which file to read. Put another name there, and the same program reads another file.