YouTip LogoYouTip

Java Nio File Move

[![Image 1: Java File](#) Java java.nio.file.Files](#) * * * The `java.nio.file.Files.move()` method is a core method in the Java NIO (New I/O) package for moving or renaming files and directories. It provides more powerful and flexible file operation capabilities than the traditional `java.io.File` class. ### Method Syntax public static Path move(Path source, Path target, CopyOption... options)throws IOException ### Parameter Description | Parameter Name | Type | Description | | --- | --- | --- | | source | Path | The source file or directory path to move | | target | Path | The target location path | | options | CopyOption... | Optional move options (variable arguments) | ### Common CopyOption Options * `StandardCopyOption.REPLACE_EXISTING`: Replace the target file if it already exists * `StandardCopyOption.ATOMIC_MOVE`: Ensure the move operation is atomic * `LinkOption.NOFOLLOW_LINKS`: Do not follow symbolic links * * * ## Method Function Details ### Basic File Move The simplest move operation, moving a file from one location to another: ## Example Path source = Paths.get("C:/temp/source.txt"); Path target = Paths.get("C:/temp2/target.txt"); Files.move(source, target); ### File Renaming The move method can also be used to rename files: ## Example Path source = Paths.get("C:/temp/oldname.txt"); Path target = Paths.get("C:/temp/newname.txt"); Files.move(source, target); ### Directory Move Move an entire directory (including its contents): ## Example Path sourceDir = Paths.get("C:/temp/mydir"); Path targetDir = Paths.get("C:/temp2/mydir"); Files.move(sourceDir, targetDir); * * * ## Advanced Usage ### Overwriting Existing Files Use the `REPLACE_EXISTING` option to overwrite an existing target file: ## Example Path source = Paths.get("C:/temp/source.txt"); Path target = Paths.get("C:/temp2/target.txt"); Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); ### Atomic Move Ensure the move operation is atomic (either completely successful or completely failed): ## Example Path source = Paths.get("C:/temp/source.txt"); Path target = Paths.get("C:/temp2/target.txt"); Files.move(source, target, StandardCopyOption.ATOMIC_MOVE); * * * ## Exception Handling The `move()` method may throw the following exceptions: * `IOException`: Thrown when an I/O error occurs * `FileAlreadyExistsException`: Thrown when the target file already exists and `REPLACE_EXISTING` option is not specified * `DirectoryNotEmptyException`: Thrown when trying to move a non-empty directory to a different file system Recommended exception handling approach: ## Example 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("Error moving file: "+ e.getMessage()); e.printStackTrace(); } * * * ## Notes 1. **Cross-filesystem move**: When moving files between different file systems, it is actually a copy+delete operation 2. **Symbolic links**: Follows symbolic links by default, unless `LinkOption.NOFOLLOW_LINKS` is specified 3. **Directory move restrictions**: Cannot move a directory to itself or its subdirectories 4. **File attributes**: Move operations generally preserve the basic attributes of the file 5. **Performance considerations**: Moving within the same file system is usually much faster than moving across file systems * * * ## Practical Application Examples ### Safe File Move ## Example public static boolean safeMoveFile(Path source, Path target){ try{ // Try atomic move Files.move(source, target, StandardCopyOption.ATOMIC_MOVE); return true; }catch(AtomicMoveNotSupportedException e){ try{ // When atomic move is not supported, use regular move + overwrite Files.move(source, target, StandardCopyOption.REPLACE_EXISTING); return true; }catch(IOException ex){ System.err.println("Failed to move file: "+ ex.getMessage()); return false; } }catch(IOException e){ System.err.println("Failed to move file: "+ e.getMessage()); return false; } } ### Batch Move Files ## Example public static void batchMoveFiles(Path sourceDir, Path targetDir, String filePattern) throws IOException{ try(DirectoryStream stream = Files.newDirectoryStream(sourceDir, filePattern)){ for(Path sourceFile : stream){ Path targetFile = targetDir.resolve(sourceFile.getFileName()); Files.move(sourceFile, targetFile); } } } [![Image 2: Java File](#) Java java.nio.file.Files](#)
← Java Nio File DeleteifexistsJava Nio File β†’