编写一个Java应用程序,使用Java的输入输出流技术将Input.txt的内容(Input.txt为文本文件)逐行读出,每读出一行就顺序为其添加行号(从1开始,逐行递增),并写入到另一个文本文件Output.txt中。
1 package com.tiger.practice; 2 import java.io.BufferedReader; 3 import java.io.BufferedWriter; 4 import java.io.FileNotFoundException; 5 import java.io.FileReader; 6 import java.io.FileWriter; 7 import java.io.IOException; 8 9 public class Homework6 { 10 11 public static void main(String[] args) { 12 // TODO Auto-generated method stub 13 BufferedReader br = null; 14 BufferedWriter bw = null; 15 try { 16 br = new BufferedReader(new FileReader("D:\10 cd onedrive\OneDrive - mail.cdu.edu.cn\2 大学文档\02 secondary docu\01 学科文档\15 Java\Input.txt")); 17 bw = new BufferedWriter(new FileWriter("D:\10 cd onedrive\OneDrive - mail.cdu.edu.cn\2 大学文档\02 secondary docu\01 学科文档\15 Java\Output.txt")); 18 String str = ""; 19 int i = 1; 20 while((str = br.readLine()) != null)//read a line 21 { 22 bw.write(i+" "); 23 bw.write(str); 24 System.out.println(str); 25 bw.newLine(); 26 i++; 27 } 28 bw.flush(); 29 bw.close(); 30 br.close(); 31 32 } catch (FileNotFoundException e) { 33 // TODO Auto-generated catch block 34 System.out.println("找不到指定文件!"); 35 }catch (IOException e) { 36 // TODO Auto-generated catch block 37 e.printStackTrace(); 38 } 39 } 40 41 }