用java将C盘hello.txt文件写入D盘,文件名为newHello.txt
时间: 2024-05-09 08:21:05 浏览: 121
以下是Java代码实现:
```java
import java.io.*;
public class CopyFileExample {
public static void main(String[] args) {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
try {
// 1. 创建输入流和输出流对象
File inputFile = new File("C:\\hello.txt");
File outputFile = new File("D:\\newHello.txt");
inputStream = new FileInputStream(inputFile);
outputStream = new FileOutputStream(outputFile);
// 2. 读取输入流并写入输出流
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
// 3. 关闭输入流和输出流
inputStream.close();
outputStream.close();
System.out.println("文件复制成功!");
} catch (IOException e
阅读全文