About Question enthuware.ocpjp.v8.2.1538 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
lveto15
Posts: 27
Joined: Tue Sep 22, 2015 1:01 pm
Contact:

About Question enthuware.ocpjp.v8.2.1538 :

Post 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.

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post 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.
If you like our products and services, please help us by posting your review here.

lveto15
Posts: 27
Joined: Tue Sep 22, 2015 1:01 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post 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".

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post 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.
If you like our products and services, please help us by posting your review here.

lveto15
Posts: 27
Joined: Tue Sep 22, 2015 1:01 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post by lveto15 »

Inadequate test data = incorrect conclusion :).

Thank you for the detailed explanation.

javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post 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).

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post 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.
If you like our products and services, please help us by posting your review here.

javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post 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!

javalass
Posts: 54
Joined: Thu Mar 03, 2016 1:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post by javalass »

The default of CREATE, TRUNCATE_EXISTING, and WRITE only applies if you pass no OpenOptions to the method.

asyzdykov
Posts: 1
Joined: Sun Dec 29, 2019 12:59 pm
Location: Omsk
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post 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?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1538 :

Post by admin »

StandardOpenOption.CREATE only creates a new file if a file does not exit. This is not platform specific.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 49 guests