Page 1 of 1
About Question enthuware.ocpjp.v8.2.1805 :
Posted: Wed May 18, 2016 12:01 am
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
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Wed May 18, 2016 3:33 am
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.
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Fri May 20, 2016 12:45 am
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
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Fri May 20, 2016 2:14 am
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.
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Fri May 20, 2016 12:27 pm
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
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Fri May 20, 2016 9:57 pm
by admin
Cool

That is a standard advice I give to everyone. No offence intended.
-Paul.
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Fri Jul 01, 2016 12:04 am
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?
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Fri Jul 01, 2016 2:49 am
by admin
Implementation specific means it depends on the implementation of the JVM, which in turn depends on os and file system.
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Mon Jul 04, 2016 6:46 pm
by johnlong
Does this mean, that we never be sure if file would be replaced or exception would be thrown in this case?
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Mon Jul 04, 2016 8:39 pm
by admin
Yes, that is correct.
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Wed Jul 06, 2016 5:26 pm
by johnlong
Thanks
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Thu Jul 26, 2018 3:27 am
by ArpRokz
I think the code also has a possibility to throw a java.nio.file.AtomicMoveNotSupportedException
Right ?
Re: About Question enthuware.ocpjp.v8.2.1805 :
Posted: Thu Jul 26, 2018 4:25 am
by admin
Yes, that is possible.