使用Java的输入输出流技术将Input.txt的内容逐行读出,每读出一行就顺序为其添加行号(从1开始,逐行递增),并写入到另一个文本文件Output.txt中

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class IOtest02 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader("Input.txt"));
			bw = new BufferedWriter(new FileWriter("Output.txt"));
			String str = "";
			int i = 1;
			while((str = br.readLine()) != null){
				bw.write(i+" ");
				bw.write(str);
				System.out.println(str);
				bw.newLine();
				i++;
			}
			bw.flush();
			bw.close();
			br.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("找不到指定文件!");
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}

你可能感兴趣的:(Java基础)