为以上代码添加详细注释
时间: 2024-03-07 08:50:43 浏览: 20
好的,以下是添加了详细注释的代码:
```java
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
// 判断参数是否正确
if (args.length != 2) { // 参数数量不为2,输出用法提示
System.out.println("Usage: java FileCopy source_file target_file");
return;
}
// 获取源文件名和目标文件名
String sourceFileName = args[0]; // 第一个参数为源文件名
String targetFileName = args[1]; // 第二个参数为目标文件名
// 创建输入输出流
try {
FileInputStream fis = new FileInputStream(sourceFileName); // 创建输入流,读取源文件
FileOutputStream fos = new FileOutputStream(targetFileName); // 创建输出流,写入目标文件
// 读写文件
byte[] buffer = new byte[1024]; // 创建缓存数组
int len; // 记录每次读取的字节数
while ((len = fis.read(buffer)) != -1) { // 读取源文件内容到缓存数组中
fos.write(buffer, 0, len); // 将缓存数组中的内容写入目标文件中
}
// 关闭输入输出流
fis.close();
fos.close();
System.out.println("File copied successfully!"); // 输出复制成功提示
} catch (IOException e) { // 捕获IO异常
e.printStackTrace(); // 输出异常信息
}
}
}
```
希望这样更容易理解了。