[OCPJP]複製檔案

Given the code fragment:

and assuming that the file /data/december/log.txt is accessible and contains:

10-Dec-2014 – Executed successfully

What is the result?

A. A file with the name log.txt is created in the /data directory and the content of the /data/december/log.txt file is copied to it.
B. The program executes successfully and does NOT change the file system.
C. A FileNotFoundException is thrown at run time.
D. A FileAlreadyExistsException is thrown at run time.

題解

若/data已存在,程式會拋出FileAlreadyExistsException。若/data不存在,會在「/」目錄下產生「data」檔案,內容為「10-Dec-2014 – Executed successfully」。

所以這題選擇選項D的話會比較合適。

若要避免FileAlreadyExistsException,可以在Files類別的copy方法加入CopyOption至第三個參數中,如下:

關於作者

Magic Len

各位好,我是Magic Len,是這網站的管理員。我是台灣台中大肚山上人,畢業於台中高工資訊科和台灣科技大學資訊工程系,曾在桃機航警局服役。我熱愛自然也熱愛科學,喜歡和別人分享自己的知識與經驗。如果你有興趣認識我,可以加我的Facebook,並且請註明是從MagicLen來的。

相關文章