给下列代码加上注释import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Ch8 { public static void main(String[] args) { String inputFile = "D:\\eclipse-workspace\\input.txt"; // 输入文件名 String outputFile = "D:\\eclipse-workspace\\output.txt"; // 输出文件名 try (BufferedReader reader = new BufferedReader(new FileReader(inputFile)); BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) { String line; int lineNumber = 0; while ((line = reader.readLine()) != null) { lineNumber++; writer.write(lineNumber + ": " + line); writer.newLine(); // 换行 } } catch (IOException e) { e.printStackTrace(); } } }
时间: 2023-06-27 21:05:19 浏览: 149
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Ch8 {
public static void main(String[] args) {
String inputFile = "D:\\eclipse-workspace\\input.txt"; // 输入文件名
String outputFile = "D:\\eclipse-workspace\\output.txt"; // 输出文件名
try (BufferedReader reader = new BufferedReader(new FileReader(inputFile)); // 创建一个读取输入文件的 BufferedReader 对象
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile))) { // 创建一个写入输出文件的 BufferedWriter 对象
String line;
int lineNumber = 0; // 初始化行号为 0
while ((line = reader.readLine()) != null) { // 逐行读取输入文件的内容
lineNumber++; // 行号加 1
writer.write(lineNumber + ": " + line); // 将行号和读取到的内容写入输出文件
writer.newLine(); // 换行
}
} catch (IOException e) { // 捕获可能出现的 IO 异常
e.printStackTrace(); // 打印异常信息
}
}
}
阅读全文