Page 1 of 1
About Question enthuware.ocpjp.v8.2.1538 :
Posted: Wed Oct 21, 2015 3:06 am
by lveto15
The third options has a bit misleading explanation:
WRITE will cause an exception to be thrown if the file does not exists. It will not get rid of the existing content either.
I have tested this option, and if there is empty file then the first run will fill it with data. The second run repaces this data with the new one, e.g.:
after the first run
Testing...2015-10-21T10:00:48.259
after the second run
Testing...2015-10-21T10:05:10.106
Still only one line - so the previous content has been replaced.
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Wed Oct 21, 2015 3:31 am
by admin
Not sure how you tested but it throws a java.nio.file.FileAlreadyExistsException when the file already exists, which is what the explanation says as well.
Paul.
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Wed Oct 21, 2015 8:16 am
by lveto15
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".
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Wed Oct 21, 2015 8:40 am
by admin
Sorry, I was looking at option 4.
Regarding option 3, the part of the explanation that you've quoted is correct as well. The code given in this option will overwrite the existing content. For example, if the file had 10 bytes and if you write 1 byte using this code, only the first byte will be replaced. The rest of the 9 bytes will not be removed. The problem statement clearly says, "(the content of the existing file, if any, should go away)". This option will not do that.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Thu Oct 22, 2015 2:03 am
by lveto15
Inadequate test data = incorrect conclusion

.
Thank you for the detailed explanation.
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Mon Apr 04, 2016 6:20 am
by javalass
The default behaviour is CREATE, TRUNCATE_EXISTING, and WRITE.
When I tested it, options 1, 3 and 5 all worked as expected (i.e. truncated the text on an existing file).
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Mon Apr 04, 2016 10:28 pm
by admin
I tried it just now with option 1 an 3, they did not truncate existing file. I used the same code given in the explanation.
Can you please post the code that you tested?
-Paul.
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Tue Apr 05, 2016 9:11 am
by javalass
Oops, you're right, they don't truncate. I too used bad examples for my text files, so I couldn't see that it was overwriting. Testing it again with a target file full of data, I can see that 1 and 3 clearly don't truncate.
Sorry about that!
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Tue Apr 05, 2016 9:13 am
by javalass
The default of CREATE, TRUNCATE_EXISTING, and WRITE only applies if you pass no OpenOptions to the method.
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Mon Dec 30, 2019 10:01 am
by asyzdykov
Path myfile = Paths.get("c:\\temp\\test.txt");
BufferedWriter br = Files.newBufferedWriter(myfile, Charset.forName("UTF-8"),
new OpenOption[] {StandardOpenOption.CREATE});
first option
replaces the content or create new file if not exist...
is it platform dependent behaviour?
Re: About Question enthuware.ocpjp.v8.2.1538 :
Posted: Mon Dec 30, 2019 10:21 am
by admin
StandardOpenOption.CREATE only creates a new file if a file does not exit. This is not platform specific.