About Question enthuware.ocpjp.v8.2.1805 :

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

Moderator: admin

Post Reply
schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

About Question enthuware.ocpjp.v8.2.1805 :

Post by schchen2000 »

By default, Files.move method attempts to move the file to the target file, failing if the target file exists except if the source and target are the same file, in which case this method has no effect. Therefore, this code should throw an exception because a.java exists in the target directory.
Source is represented by p1 while destination is represented by p2 in this problem.

"....failing if the target file exist...."

It did not say the target file exists by the same name as the source file or different name from the source file. If the target file exist, move fails. That's what it says by the bold text above. There is an exception, i.e. in case of source and destination files being the same, there won't be a failure. In case of source and destination files being the same, move() method has no effect, i.e. it's like running a no-op.

Given all that information and analysis, why did you say

"Therefore, this code should throw an exception because a.java exists in the target directory."????

When source and destination files are the same, shouldn't move() method have no effect? Why throw an exception?

I fully understand why java.nio.file.NoSuchFileException is thrown due to running deletion after move.

I tried to understand this topic by reading Oracle documentation. It's so verbose and a lot of rubbish so I finally resorted to asking the question on your site.

Many thanks for your time.

Schmichael

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

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

Post by admin »

Source and target files are not same. Source is c:\\pathtest\\a.java while destination is c:\\pathtest\\dir2\\a.java

Same would be if you have paths p1 and p2 both pointing to the same file.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

admin wrote:Source and target files are not same. Source is c:\\pathtest\\a.java while destination is c:\\pathtest\\dir2\\a.java

Same would be if you have paths p1 and p2 both pointing to the same file.
When you said
Same would be if you have paths p1 and p2 both pointing to the same file
you meant both p1 and p2 points to the same file residing at the same physical location, i.e. one physical file pointed by 2 pointers.

Is that correct?
There are two contradictory factors at play here.

1. By default, Files.move method attempts to move the file to the target file, failing if the target file exists
When you said "failing if the target file exists,"

did you mean the move() methods fails (i.e. throws an exception) if there exists a target file that may have the same name as or different from the name of the source file?

Thanks.

Schmichael

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

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

Post by admin »

schchen2000 wrote:
admin wrote:Source and target files are not same. Source is c:\\pathtest\\a.java while destination is c:\\pathtest\\dir2\\a.java

Same would be if you have paths p1 and p2 both pointing to the same file.
Yes.
When you said
Same would be if you have paths p1 and p2 both pointing to the same file
you meant both p1 and p2 points to the same file residing at the same physical location, i.e. one physical file pointed by 2 pointers.

Is that correct?
Yes.

[quote[
There are two contradictory factors at play here.

1. By default, Files.move method attempts to move the file to the target file, failing if the target file exists
When you said "failing if the target file exists,"

did you mean the move() methods fails (i.e. throws an exception) if there exists a target file that may have the same name as or different from the name of the source file?

Thanks.

Schmichael[/quote]
"same file" implies path+name are same i.e. same physical file.
"files with same name" just means two files with same name. If their paths are different, then they are two different files.


So now, if you try to make a move where the target file is same as the source file (NOTICE - SAME FILE), the method returns without doing anything.
If you try to make a move where the target file exists but is not the same file, you will get an exception.
Hope that is clear.

BTW, you should try writing some code and see what happens.

-Paul.

schchen2000
Posts: 106
Joined: Mon Mar 28, 2016 11:36 pm
Contact:

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

Post by schchen2000 »

Thanks a lot, Paul. It's clear now. I've tried writing some code on the side for some of my other questions I previously asked.

I'm not sure if you could remember as there are so many people asking you questions all the time. I, however, did not write code to test this concept. In hindsight, I should have. Thanks again.

Schmichael

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

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

Post by admin »

Cool :) That is a standard advice I give to everyone. No offence intended.
-Paul.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post by johnlong »

Hi

Java API says
The move is performed as an atomic file system operation and all other options are ignored. If the target file exists then it is implementation specific if the existing file is replaced or this method fails by throwing an IOException. If the move cannot be performed as an atomic file system operation then AtomicMoveNotSupportedException is thrown.
What does it mean exactly "it is implementation specific" - it it platform/JVM/filesystem specific?

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

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

Post by admin »

Implementation specific means it depends on the implementation of the JVM, which in turn depends on os and file system.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post by johnlong »

Does this mean, that we never be sure if file would be replaced or exception would be thrown in this case?

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

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

Post by admin »

Yes, that is correct.

johnlong
Posts: 197
Joined: Mon Jun 20, 2016 5:06 pm
Contact:

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

Post by johnlong »

Thanks

ArpRokz
Posts: 15
Joined: Sun Jan 28, 2018 12:38 pm
Contact:

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

Post by ArpRokz »

I think the code also has a possibility to throw a java.nio.file.AtomicMoveNotSupportedException
Right ?

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

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

Post by admin »

Yes, that is possible.

Post Reply

Who is online

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