Write to a file with BufferedWriter and java.nio.file.Files, select CharSet in Java
Description
The following code shows how to write to a file with BufferedWriter and java.nio.file.Files, select CharSet.
Example
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;
//from w w w . j av a 2s.c o m
public class Main {
public static void main(String[] args) throws IOException {
String newName = "asdf";
Path file = Paths.get("/users.txt");
try (BufferedWriter writer = Files.newBufferedWriter(file, Charset.defaultCharset(), StandardOpenOption.APPEND)) {
writer.newLine();
writer.write(newName, 0, newName.length());
}
}
}