Given the following code fragment:
10. p1 = paths.get(“report.txt”);
11. p2 = paths.get(“company”);
12. / / insert code here
Which code fragment, when inserted independently at line 12, move the report.txt file to the
company directory, at the same level, replacing the file if it already exists?
A.
Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
B.
Files.move(p1, p2, StandardCopyOption.REPLACE_Existing, LinkOption.NOFOLLOW_LINKS);
C.
Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING,
LinkOption.NOFOLLOW_LINKS);
D.
Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.copy_ATTRIBUTES,
StandrardCopyOp)
E.
Files.move (p1, p2, StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.copy_ATTRIBUTES,
LinkOption.NOF)
Explanation:
Moving a file is equally as straight forward
move(Path source, Path target, CopyOption… options);
The available StandardCopyOptions enums available are:
StandardCopyOption.REPLACE_EXISTING
StandardCopyOption.ATOMIC_MOVE
If Files.move is called with StandardCopyOption.COPY_ATTRIBUTES an
UnsupportedOperationException isthrown.
The answer is A only.
C is not correct; accroding to Java API of Files.move(), only REPLACE_EXISTING and ATOMIC_MOVE are legal arguments.
Aswer: A and C
public static void main(String[] args) throws IOException {
Path p1 = Paths.get(“C:\\java7\\faculty\\report.txt”);
Path p2 = Paths.get(“C:\\java7\\faculty\\report.txt”);
Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING, LinkOption.NOFOLLOW_LINKS);
//Exception
//Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
//Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES, LinkOption.NOFOLLOW_LINKS);
}
}
A,C >> move(Path source, Path target, CopyOption… options)
Both LinkOption and StandardCopyOption implementing CopyOption interface.
A
Actually, none of them.
I ran the code A and the code C.
They delete company directory and rename report.txt file to company FILE!
Only A
MOVE
h t t p s : / / docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#move(java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption…)
The options parameter may include any of the following:
REPLACE_EXISTING
ATOMIC_MOVE
COPY
h t t p s : / / docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy(java.io.InputStream,%20java.nio.file.Path,%20java.nio.file.CopyOption…)
The options parameter may include any of the following:
REPLACE_EXISTING
ATOMIC_MOVE
NOFOLLOW_LINKS
Sorry, A and C
For windows (Move or rename a file to a target file)
p1 = Paths.get(“D:\\TEMP\\report.txt”);
p2 = Paths.get(“D:\\TEMP\\company\\report.txt”);
Files.move (p1, p2, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
Files.move (p1, p2, StandardCopyOption.REPLACE_EXISTING, LinkOption.NOFOLLOW_LINKS);
public static Path move (
Path source,
Path target,
CopyOption… options) throws IOException
MOVE :
The options parameter (Interface CopyOption) may include any of the following
REPLACE_EXISTING, ATOMIC_MOVE
An implementation of this interface may support additional implementation specific options
Interface CopyOption
h t t p s : / / docs.oracle.com/javase/7/docs/api/java/nio/file/CopyOption.html
public interface CopyOption
An object that configures how to copy or move a file.
All Known Implementing Classes: LinkOption, StandardCopyOption
Enum StandardCopyOption: The StandardCopyOption enumeration type defines the standard options.
public enum StandardCopyOption extends Enum implements CopyOption
Enum LinkOption : Defines the options as to how symbolic links are handled.
public enum LinkOption extends Enum implements OpenOption, CopyOption
a