Hm, it throws an exception if file
does not exist. Maybe we are talking about different part?
I mean the second part of the explanation:
It will not get rid of the existing content either.
The procedure to test it was as following:
1. empty file optionTest.txt was created manually
2. class was run, as result content as described was in the file
3. class was run second time. The content was changed to the new one. Previous was rid off.
Below code of the class
Code: Select all
public static void main(String... args){
writeToFile(Paths.get("optionTest.txt"), new OpenOption[]{StandardOpenOption.WRITE});
}
private static void writeToFile(Path path, OpenOption[] options){
System.out.println("try with options: "+Arrays.toString(options));
try(
BufferedWriter br = Files.newBufferedWriter(path, Charset.forName("UTF-8"), options);
){
br.write("Testing..."+LocalDateTime.now());
} catch (Exception e){
System.out.println(e);
}
}
I checked it again, this time put "One" as the initial content. After runing the content is "Testing...2015-10-21T15:13:26.518".