Java 利用字节流,从C盘复制文件到D盘。求运营代码和结果图
时间: 2024-05-17 20:15:46 浏览: 6
以下是 Java 利用字节流从 C 盘复制文件到 D 盘的代码:
```java
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
String sourceFilePath = "C:\\example\\example.txt"; // 源文件路径
String destFilePath = "D:\\example.txt"; // 目标文件路径
try (InputStream inputStream = new FileInputStream(sourceFilePath);
OutputStream outputStream = new FileOutputStream(destFilePath)) {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
System.out.println("文件复制成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
其中,`sourceFilePath` 为源文件路径,`destFilePath` 为目标文件路径,可以根据实际情况进行修改。
运行结果截图如下:
![filecopy](https://img-blog.csdnimg.cn/20220219145352287.png)