将文本1的内容写入到文本2中

package com.pdcss.io;

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

public class IOTest {

    /**
     * file1>>file2
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        File infile = new File("D:\\1.txt");//这个必须有,自己在里面写点内容
        File outfile = new File("D:\\2.txt");//可以没有,会自动建
        FileReader fr = null ;
        FileWriter fw = null;
        try {
            fr = new FileReader(infile);
            fw = new FileWriter(outfile);
            
            BufferedReader br = new BufferedReader(fr);
            BufferedWriter bw = new BufferedWriter(fw);
            
            String line = null;
            String[] oStr = null;
            
            while((line=br.readLine())!= null){
                bw.write(line+System.getProperty("line.separator"));
            }
            bw.close();
            br.close();
            fw.close();
            fr.close();
            
        } catch (FileNotFoundException e) {
            System.out.println("文件找不到");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("写入失败");
            e.printStackTrace();
        }
        System.out.println("写入成功");
    }

}

posted @ 2013-12-05 17:53  搜索中...  阅读(187)  评论(0编辑  收藏  举报