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?

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?

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 is thrown.



Leave a Reply 8

Your email address will not be published. Required fields are marked *


Marian

Marian

both A and C work fine

Marian

Marian

A will not work

GoodLuck

GoodLuck

Using windows xp for testing.

A produce the following exception :

java.nio.file.AccessDeniedException: report.txt -> company
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:301)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:286)
at java.nio.file.Files.move(Files.java:1345)
at testapp02.TestApp02.Test2(TestApp02.java:79)
at testapp02.TestApp02.main(TestApp02.java:31)

C is successful but made the company folder into a file that cannot be opened.

Charles

Charles

c is for files.copy() so only A is correct

gelete

gelete

A

MOVE
https://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
https://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

gelete

gelete

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

gelete

gelete

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