Java File Write Difference

1

I would like to know the difference in these two methods of writing to files.

Path path = Paths.get("E:/documentos/texte.txt");

Charset utf8 = StandardCharsets.UTF_8;

try(BufferedWriter escrever = Files.newBufferedWriter(path, utf8)) {
    escrever.write(" ");
} 

For this:

File arq = new File("E:/documentos.texte.txt");
FileWriter fw = new FileWriter(arq);

try(BufferedWriter escrever = new BufferedWriter(fw)) {         
    escrever.write(" ");
}
    
asked by anonymous 21.11.2016 / 19:03

2 answers

1

Directly, Path is more modern and does everything File does in a better way. In new projects it is recommended to use Path.

For more information on this link .

    
21.11.2016 / 19:18
0

I suggest using the first solution, it uses the newer File API, which has its advantages. For example, it is more practical to work with Path than String for file paths.

With regard to the final result (the generated file) there is a difference between the two solutions: in the first solution UTF-8 is used, in the second the FileWriter operating system (OS), which varies from OS to OS.

    
21.11.2016 / 19:31