Java中实现文件复制到不同磁盘的具体操作步骤是怎样的?
- 后端开发
- 2025-10-17
- 6
在Java中,将文件从一个磁盘拷贝到另一个磁盘有多种方法,以下是一些常见的方法和步骤:
使用FileInputStream和FileOutputStream
这是最基础的方法,通过读取源文件和写入目标文件来实现拷贝。
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopyExample { public static void main(String[] args) { String sourcePath = "path/to/source/file.txt"; String destPath = "path/to/destination/file.txt"; try (FileInputStream fis = new FileInputStream(sourcePath); FileOutputStream fos = new FileOutputStream(destPath)) { byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fis.read(buffer)) != 1) { fos.write(buffer, 0, bytesRead); } } catch (IOException e) { e.printStackTrace(); } } }
使用Files.copy
Java 7 引入了Files类,提供了更简洁的方法来拷贝文件。
import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.io.IOException; public class FileCopyExample { public static void main(String[] args) { String sourcePath = "path/to/source/file.txt"; String destPath = "path/to/destination/file.txt"; try { Files.copy(Paths.get(sourcePath), Paths.get(destPath), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } } }
使用Files.move
如果你希望将文件移动到另一个位置,而不是简单地拷贝,可以使用Files.move。
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.io.IOException; public class FileCopyExample { public static void main(String[] args) { String sourcePath = "path/to/source/file.txt"; String destPath = "path/to/destination/file.txt"; try { Path source = Paths.get(sourcePath); Path dest = Paths.get(destPath); Files.move(source, dest, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } } }
使用Runtime.exec
对于更复杂的拷贝任务,比如跨网络拷贝,可以使用Runtime.exec来执行系统命令。

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class FileCopyExample { public static void main(String[] args) { String sourcePath = "path/to/source/file.txt"; String destPath = "/path/to/destination/file.txt"; try { Process process = Runtime.getRuntime().exec(new String[] { "cp", sourcePath, destPath }); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } int exitCode = process.waitFor(); if (exitCode == 0) { System.out.println("File copied successfully."); } else { System.out.println("Failed to copy file."); } } catch (IOException | InterruptedException e) { e.printStackTrace(); } } }
表格对比
下面是一个简单的表格,对比了上述几种方法的优缺点:
| 方法 | 优点 | 缺点 |
|---|---|---|
| FileInputStream和FileOutputStream | 简单易懂,适用于基本文件拷贝 | 代码较多,需要手动处理异常 |
| Files.copy | 简洁,易于理解 | 无法跨网络拷贝 |
| Files.move | 用于移动文件,简洁 | 无法跨网络移动 |
| Runtime.exec | 可以执行系统命令,适用于跨网络拷贝 | 代码复杂,需要处理异常 |
FAQs
Q1:Java中如何拷贝目录?

A1: Java中可以使用Files.walk来遍历目录,并使用Files.copy方法逐个拷贝文件,以下是一个简单的示例:
import java.nio.file.*; import java.io.IOException; public class DirectoryCopyExample { public static void main(String[] args) { Path sourceDir = Paths.get("path/to/source/dir"); Path destDir = Paths.get("path/to/destination/dir"); try { Files.walk(sourceDir) .filter(Files::isRegularFile) .forEach(file > { try { Files.copy(file, destDir.resolve(sourceDir.relativize(file)), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } }); } catch (IOException e) { e.printStackTrace(); } } }
Q2:Java中如何拷贝文件时保留文件属性?
A2: 在使用Files.copy方法时,可以通过StandardCopyOption.COPY_ATTRIBUTES来保留文件属性,以下是一个示例:
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.io.IOException; public class FileCopyWithAttributesExample { public static void main(String[] args) { String sourcePath = "path/to/source/file.txt"; String destPath = "path/to/destination/file.txt"; try { Files.copy(Paths.get(sourcePath), Paths.get(destPath), StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } } }