Java I/O API – 11

      在〈Java I/O API – 11〉中尚無留言

Q001

NO.7 Given the code fragment:
Path source = Paths.get("/repo/a/a.txt");
Path destination = Paths.get("/repo");
Files.move(source, destination); // line 1
Files.delete (source); // line 2
Assuming the source file and destination folder exist, what Is the result?
A. A java.nio.file.FileAlreadyExistsException is thrown on line 1.
B. A java.nio.file.NoSuchFileException is thrown on line 2.
C. A copy of /repo/a/a.txt is moved to the /repo directory and /repo/a/a.txt is deleted.
D. a.txt is renamed repo.

Answer: A

這一題是陷井題。

不論在 Windows 或 Linux 下,File.move 或 File.copy 都會把 destination 的 “/repo” 當成是檔名,而不是把 a.txt 放到 /repo 之下。

巨匠給的給案為 B,是錯的。

Q002

NO.63 Which two expressions create a valid Java Path instance? (Choose two.)
A. Paths.get("foo")
B. Paths.getPath("too")
C. Path.get(new URI("file:///domains/oracle/test.txt"))
D. new Path("foo")
E. Paths.get(URI.create("file:///domains/oracle/test.txt"))


Answer : AE

建立 Path 實例有如下二種

Path p = Paths.get("some/path");

or

Path p = Paths.get(URI uri);

答案 E 應該是 URI,如果是 URL 就是錯誤的。

🔒 更多內容,請登入會員繼續閱讀。

立即登入

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *