Files and Paths
File Write Read
A small text file can be written, read back, and removed using Files helpers.
writeString
Files.writeString stores text at a path, creating or replacing the file.
readString
Files.readString loads the whole text file into a String.
File Write Read
FileWriteRead.java
Replay: real traced execution (multi-file project)
import java.nio.file.Files;
import java.nio.file.Path;
public class FileWriteRead {
public static void main(String[] args) throws Exception {
int score = 7;
Path path = Path.of("java21_write_read.txt");
String text = "score=" + score;
Files.writeString(path, text);
String loaded = Files.readString(path);
Files.deleteIfExists(path);
System.out.println(loaded);
}
}
import java.nio.file.Files;
import java.nio.file.Path;
public class FileWriteRead {
public static void main(String[] args) throws Exception {
int score = 3;
Path path = Path.of("java21_write_read.txt");
String text = "score=" + score;
Files.writeString(path, text);
String loaded = Files.readString(path);
Files.deleteIfExists(path);
System.out.println(loaded);
}
}
import java.nio.file.Files;
import java.nio.file.Path;
public class FileWriteRead {
public static void main(String[] args) throws Exception {
int score = 12;
Path path = Path.of("java21_write_read.txt");
String text = "score=" + score;
Files.writeString(path, text);
String loaded = Files.readString(path);
Files.deleteIfExists(path);
System.out.println(loaded);
}
}
score ← 7, path ← java21_write_read.txt, text ← score=7, loaded ← score=7
4public class FileWriteRead {5 public static void main(String[] args) throws Exception {6 int score→ 7 = 7; //@score=3, 127 Path path→ java21_write_read.txt = Path.of("java21_write_read.txt");8 String text→ score=7 = "score=" + score7;910 Files.writeString(pathjava21_write_read.txt, textscore=7);11 String loaded→ score=7 = Files.readString(pathjava21_write_read.txt);12 Files.deleteIfExists(pathjava21_write_read.txt);1314 System.out.println(loadedscore=7);15 }outputscore=7
score ← 3, path ← java21_write_read.txt, text ← score=3, loaded ← score=3
4public class FileWriteRead {5 public static void main(String[] args) throws Exception {6 int score→ 3 = 3;7 Path path→ java21_write_read.txt = Path.of("java21_write_read.txt");8 String text→ score=3 = "score=" + score3;910 Files.writeString(pathjava21_write_read.txt, textscore=3);11 String loaded→ score=3 = Files.readString(pathjava21_write_read.txt);12 Files.deleteIfExists(pathjava21_write_read.txt);1314 System.out.println(loadedscore=3);15 }outputscore=3
score ← 12, path ← java21_write_read.txt, text ← score=12, loaded ← score=12
4public class FileWriteRead {5 public static void main(String[] args) throws Exception {6 int score→ 12 = 12;7 Path path→ java21_write_read.txt = Path.of("java21_write_read.txt");8 String text→ score=12 = "score=" + score12;910 Files.writeString(pathjava21_write_read.txt, textscore=12);11 String loaded→ score=12 = Files.readString(pathjava21_write_read.txt);12 Files.deleteIfExists(pathjava21_write_read.txt);1314 System.out.println(loadedscore=12);15 }outputscore=12