How can I select Unix end of line \n
using java.nio.file.Files.write
?
Is it possible?
I do not find any option or constant to be selected.
Here is my method
import java.io.File;
//...
public void saveToFile(String absolutePath) {
File file = new File(path);
try {
Files.write(file.toPath(), lines/*List<String>*/, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
You don't really have another choice but to open a BufferedWriter
to the file and write by hand:
try (
final BufferedWriter writer = Files.newBufferedWriter(file.toPath(),
StandardCharsets.UTF_8, StandardOpenOption.APPEND);
) {
for (final String line: lines) {
writer.write(line);
writer.write('\n');
}
// Not compulsory, but...
writer.flush();
}
(or you do as @SubOptimal says; your choice!)