Java java.nio.file.Files move() メソッド
java.nio.file.Files.move()このメソッドは、Java NIO (New I/O) パッケージ内でファイルやディレクトリを移動または名前変更するための中心的なメソッドです。従来のjava.io.Fileクラスよりも強力で柔軟なファイル操作機能を提供します。
メソッド構文
public static Path move(Path source, Path target, CopyOption... options) throws IOException
パラメータの説明
| パラメータ名 | 型 | 説明 |
|---|---|---|
| source | Path | 移動するソースファイルまたはディレクトリのパス |
| target | Path | 移動先のパス |
| options | CopyOption... | 任意の移動オプション(可変長引数) |
一般的な CopyOption オプション
StandardCopyOption.REPLACE_EXISTING:ターゲットファイルが既に存在する場合は、それを置き換えますStandardCopyOption.ATOMIC_MOVE:移動操作がアトミックであることを保証しますLinkOption.NOFOLLOW_LINKS:シンボリックリンクに従いません
メソッド機能の詳細
基本的なファイル移動
最も簡単な移動操作で、ファイルをある場所から別の場所へ移動します:
例
Path source = Paths.get("C:/temp/source.txt");
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target);
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target);
ファイルの名前変更
移動メソッドはファイルの名前変更にも使用できます:
例
Path source = Paths.get("C:/temp/oldname.txt");
Path target = Paths.get("C:/temp/newname.txt");
Files.move(source, target);
Path target = Paths.get("C:/temp/newname.txt");
Files.move(source, target);
ディレクトリ移動
ディレクトリ全体(その内容を含む)を移動します:
例
Path sourceDir = Paths.get("C:/temp/mydir");
Path targetDir = Paths.get("C:/temp2/mydir");
Files.move(sourceDir, targetDir);
Path targetDir = Paths.get("C:/temp2/mydir");
Files.move(sourceDir, targetDir);
上級の使い方
既存ファイルの上書き
使用してREPLACE_EXISTINGオプションで既存のターゲットファイルを上書きします:
例
Path source = Paths.get("C:/temp/source.txt");
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
アトミックな移動
移動操作がアトミックである(完全に成功するか、完全に失敗するかのどちらか)ことを保証します:
例
Path source = Paths.get("C:/temp/source.txt");
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
例外処理
move()このメソッドは以下の例外をスローする可能性があります:
IOException:I/O エラーが発生したときにスローされますFileAlreadyExistsException:ターゲットファイルが既に存在し、指定されていない場合REPLACE_EXISTINGオプションDirectoryNotEmptyException:空でないディレクトリを異なるファイルシステムに移動しようとした場合
推奨される例外処理方法:
例
try {
Path source = Paths.get("C:/temp/source.txt");
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
System.err.println("ファイル移動中にエラーが発生しました: " + e.getMessage());
e.printStackTrace();
}
Path source = Paths.get("C:/temp/source.txt");
Path target = Paths.get("C:/temp2/target.txt");
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
System.err.println("ファイル移動中にエラーが発生しました: " + e.getMessage());
e.printStackTrace();
}
注意事項
- ファイルシステム間の移動:異なるファイルシステム間でファイルを移動する場合、実際にはコピー+削除操作になります
- シンボリックリンク:デフォルトではシンボリックリンクに従います。指定されていない限り
LinkOption.NOFOLLOW_LINKS - ディレクトリ移動の制限:ディレクトリをそれ自体またはそのサブディレクトリ内に移動することはできません
- ファイル属性:移動操作は通常、ファイルの基本属性を保持します
- パフォーマンスの考慮事項:同じファイルシステム内での移動は、通常、ファイルシステム間の移動よりもはるかに高速です
実際の応用例
安全なファイル移動
例
public static boolean safeMoveFile(Path source, Path target) {
try {
// アトミック移動を試みる
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
return true;
} catch (AtomicMoveNotSupportedException e) {
try {
// アトミック移動がサポートされていない場合は、通常の移動+上書きを使用します
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException ex) {
System.err.println("ファイルの移動に失敗しました: " + ex.getMessage());
return false;
}
} catch (IOException e) {
System.err.println("ファイルの移動に失敗しました: " + e.getMessage());
return false;
}
}
try {
// アトミック移動を試みる
Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
return true;
} catch (AtomicMoveNotSupportedException e) {
try {
// アトミック移動がサポートされていない場合は、通常の移動+上書きを使用します
Files.move(source, target, StandardCopyOption.REPLACE_EXISTING);
return true;
} catch (IOException ex) {
System.err.println("ファイルの移動に失敗しました: " + ex.getMessage());
return false;
}
} catch (IOException e) {
System.err.println("ファイルの移動に失敗しました: " + e.getMessage());
return false;
}
}
ファイルの一括移動
例
public static void batchMoveFiles(Path sourceDir, Path targetDir, String filePattern)
throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(sourceDir, filePattern)) {
for (Path sourceFile : stream) {
Path targetFile = targetDir.resolve(sourceFile.getFileName());
Files.move(sourceFile, targetFile);
}
}
}
throws IOException {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(sourceDir, filePattern)) {
for (Path sourceFile : stream) {
Path targetFile = targetDir.resolve(sourceFile.getFileName());
Files.move(sourceFile, targetFile);
}
}
}
Java java.nio.file.Files