简体   繁体   中英

create java.io.BufferedWriter using java.nio.Files or java.io classes

What is the difference between these two?

Path path = FileSystems.getDefault().getPath("file.txt");
BufferedWriter bw = Files.newBufferedWriter(path);

and

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file.txt"));

Which of these two methods is better?

The character set used by Files.newBufferedWriter is UTF8, but new FileWriter will use your Java platform "file.encoding" which may or may not be UTF8. If you are are the writer and reader of the file it's safest to assume the UTF8 versions, or supply the character encoding for either style especially if the file could be read by another computer system / in a different region with a different default character set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM