BufferReader, BufferWriter

package com.fayhong.test;

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 Stream {
	public static void main(String arg[]){
	
		try {
			BufferedReader reader= new BufferedReader(new FileReader("d:\\code.txt"));
			BufferedWriter writer=null;
			String temp=null;
			try {
				 writer=new BufferedWriter(new FileWriter("d:\\test.txt",true));//true表示往文件后面写,不会覆盖原有内容
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			try {
			
				while( (temp=reader.readLine())!=null){
					System.out.println(temp);
					writer.write(temp);
					writer.newLine();
				}
				writer.flush();////////////
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} 
		
	}
}

你可能感兴趣的:(java)