使用BufferedWriter类写入文件
// 来 自 时 代 J a v a - N o w J a v a . c o m import java.io.BufferedWriter; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class Main { public static void main(String[] args) throws IOException { try { String newName = "Vivian"; Path file = Paths.get("/home/docs/users.txt"); try (BufferedWriter writer = Files.newBufferedWriter(file, Charset.defaultCharset(), StandardOpenOption.APPEND)) { writer.newLine();