Write a String to a file with one line.
Porównanie kodu
✕ Java 8
try (FileWriter fw =
new FileWriter("out.txt");
BufferedWriter bw =
new BufferedWriter(fw)) {
bw.write(content);
}
✓ Java 11+
Files.writeString(
Path.of("out.txt"),
content
);
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
One line
No writer wrapping or try-with-resources needed.
Safe defaults
UTF-8 encoding, proper file handle cleanup.
Options
Pass OpenOption flags for append, create, etc.
Stare podejście
FileWriter + BufferedWriter
Nowoczesne podejście
Files.writeString()
Od JDK
11
Poziom trudności
Początkujący
Wsparcie JDK
Writing files
Dostępne
Widely available since JDK 11 (Sept 2018)
Jak to działa
Files.writeString() writes content to a file with UTF-8 encoding by default. Options can be passed for appending, creating, etc.
Powiązana dokumentacja