Java 學習記錄92 — copy file

張小雄
1 min readDec 10, 2021

這邊創了三個資料夾,跟好幾個文檔,各層級關係可以到下方我的 Github 去看

package nonBlockingIO.path.copyExamples;import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
public class Main {
public static void main(String[] args) {
try {
Path sourceFile = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "file1.txt");
Path copyFile = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "file1copy.txt");
Files.copy(sourceFile, copyFile);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
}
}

copy()不難看出來,這是複製檔案的功能

但如果已經執行過,被複製的檔案已經存在

則會報錯

java.nio.file.FileAlreadyExistsException: src\nonBlockingIO\path\copyExamples\file1copy.txt

at java.base/sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:123)

at java.base/sun.nio.fs.WindowsFileSystemProvider.copy(WindowsFileSystemProvider.java:283)

at java.base/java.nio.file.Files.copy(Files.java:1295)

at nonBlockingIO.path.copyExamples.Main.main(Main.java:13)

java.nio.file.FileAlreadyExistsException: src\nonBlockingIO\path\copyExamples\file1copy.txt

如果想避免因為檔案存在而報錯,可以在copy()加入第三個參數

Files.copy(sourceFile, copyFile, StandardCopyOption.REPLACE_EXISTING);

這樣就不用管檔案存不存在,可以確保順利執行複製檔案

Path sourceFile = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples","Dir1");
Path copyFile = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples","Dir4");
Files.copy(sourceFile, copyFile, StandardCopyOption.REPLACE_EXISTING);

這樣則是複製資料夾,但是空的資料夾,裡面的檔案沒有一起被複製

Path fileToMove = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "file1copy.txt");
Path destination = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "Dir1", "file1copy.txt");
Files.move(fileToMove, destination);

這樣是移動檔案

一樣可以在第三個參數加上 StandardCopyOption.REPLACE_EXISTING

// rename
Path fileToMove = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "Dir1", "file1copy.txt");
Path destination = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "Dir1", "file1copyRename.txt");
Files.move(fileToMove, destination);

這樣可以把檔案改名

Path fileToDelete = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "Dir1", "file1copyRename.txt");
Files.delete(fileToDelete);

這樣是刪除檔案

但如果執行第二次

輸出結果:

java.nio.file.NoSuchFileException: src\nonBlockingIO\path\copyExamples\Dir1\file1copyRename.txt

at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:85)

at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)

at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)

at java.base/sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:274)

at java.base/sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:105)

at java.base/java.nio.file.Files.delete(Files.java:1142)

at nonBlockingIO.path.copyExamples.Main.main(Main.java:33)

src\nonBlockingIO\path\copyExamples\Dir1\file1copyRename.txt

因為檔案已經被刪除,又要執行刪除就會報錯

改成這樣就不用擔心了

Files.deleteIfExists(fileToDelete);

Path fileToCreate = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "file2.txt");
Files.createFile(fileToCreate);

有了刪除檔案,當然也有新增檔案

Path dirToCreate = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "Dir4");
Files.createDirectory(dirToCreate);

自然也有新增資料夾

Path dirToCreate = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples", "Dir2/Dir3/Dir4/Dir5/Dir6");
Files.createDirectories(dirToCreate);

想要創造多層級的資料夾就直接列出來

這邊用的是複數 createDirectories()

Path dirToCreate = FileSystems.getDefault().getPath("src/nonBlockingIO/path/copyExamples/Dir2/Dir3/Dir4/Dir5/Dir6/Dir7");
Files.createDirectories(dirToCreate);

也能直接寫成一行

上面代碼全都紀錄在我的 Github

--

--