YouTip LogoYouTip

Java Nio File Copy

Java java.nio.file.Files copy() Method | Rookie Tutorial\n\n[![Image 1: Java File](#) Java java.nio.file.Files](#)\n\n* * *\n\n`java.nio.file.Files.copy()` is a utility method provided in the Java NIO (New Input/Output) package, used for copying files or directories between file systems. This method offers more efficient and flexible file operation capabilities compared to the traditional `java.io` package.\n\n### Method Overloading\n\nThe `Files.copy()` method has three main overloaded forms:\n\n#### Copy from input stream to file\n\npublic static long copy(InputStream in, Path target, CopyOption... options)throws IOException\n\n#### Copy from file to output stream\n\npublic static long copy(Path source, OutputStream out)throws IOException\n\n#### File to file copy\n\npublic static Path copy(Path source, Path target, CopyOption... options)throws IOException\n\n### Parameter Details\n\n#### Input Parameters\n\n* **InputStream in**: The input stream to copy from\n* **Path source**: The source file path\n* **Path target**: The target file path\n* **OutputStream out**: The output stream to write to\n* **CopyOption... options**: Optional copy options\n\n#### CopyOption Options\n\nCommon `CopyOption` options include:\n\n* `StandardCopyOption.REPLACE_EXISTING`: Replace the target file if it already exists\n* `StandardCopyOption.COPY_ATTRIBUTES`: Copy file attributes to the new file\n* `LinkOption.NOFOLLOW_LINKS`: Do not follow symbolic links\n\n### Return Value\n\n* For stream-to-file copy: Returns the number of bytes copied\n* For file-to-file copy: Returns the target file path\n* For file-to-stream copy: Returns the number of bytes copied\n\n* * *\n\n## Usage Examples\n\n### Basic File Copy\n\n## Example\n\nPath source = Paths.get("source.txt");\n\n Path target = Paths.get("target.txt");\n\ntry{\n\n Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);\n\nSystem.out.println("File copied successfully");\n\n}catch(IOException e){\n\nSystem.err.println("Copy failed: "+ e.getMessage());\n\n}\n\n### File Copy with Attributes\n\n## Example\n\nPath source = Paths.get("source.txt");\n\n Path target = Paths.get("target.txt");\n\ntry{\n\n Files.copy(source, target, \n\nStandardCopyOption.REPLACE_EXISTING,\n\nStandardCopyOption.COPY_ATTRIBUTES);\n\nSystem.out.println("File and attributes copied successfully");\n\n}catch(IOException e){\n\nSystem.err.println("Copy failed: "+ e.getMessage());\n\n}\n\n### Input Stream to File Copy\n\n## Example\n\ntry(InputStream in =new URL("http://example.com/file.txt").openStream()){\n\n Path target = Paths.get("downloaded.txt");\n\nlong bytesCopied = Files.copy(in, target);\n\nSystem.out.println("Download completed, copied "+ bytesCopied +" bytes");\n\n}catch(IOException e){\n\nSystem.err.println("Download failed: "+ e.getMessage());\n\n}\n\n* * *\n\n## Important Notes\n\n1. **Directory Copy**: The `Files.copy()` method does not recursively copy directory contents by default; it only copies empty directories\n2. **Symbolic Links**: By default, it follows symbolic links unless `LinkOption.NOFOLLOW_LINKS` is specified\n3. **Atomicity**: File copy operations are not atomic and may fail during the process\n4. **Performance**: For large files, NIO copying is typically more efficient than traditional IO\n5. **Exception Handling**: Must handle potential `IOException` exceptions\n\n* * *\n\n## Comparison with Traditional IO\n\n| Feature | NIO Files.copy() | Traditional IO (FileInputStream/FileOutputStream) |\n| --- | --- | --- |\n| Performance | Higher | Lower |\n| Feature Richness | More comprehensive | Basic |\n| Exception Handling | More concise | More cumbersome |\n| Symbolic Link Handling | Supported | Not supported |\n| File Attribute Copying | Supported | Not supported |\n\n* * *\n\n## Best Practices\n\n1. For simple file copying, use the most basic form of `Files.copy()`\n2. When preserving file attributes is needed, add the `COPY_ATTRIBUTES` option\n3. When dealing with potentially existing target files, use `REPLACE_EXISTING`\n4. For large files, consider using `Files.copy()` instead of traditional IO methods\n5. Always use try-with-resources to handle stream resources\n\n[![Image 2: Java File](#) Java java.nio.file.Files](#)
← Java Nio File DeleteJava File Tostring β†’