集册 Java实例教程 使用BufferedReader和PrintWriter逐行复制文件

使用BufferedReader和PrintWriter逐行复制文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

519
使用BufferedReader和PrintWriter逐行复制文件

import java.io.FileReader;

import java.io.FileWriter;

import java.io.BufferedReader;
/*
n o w    j a v a  . c o m 提 供
*/

import java.io.PrintWriter;

import java.io.IOException;


public class CopyLines {

    public static void main(String[] args) throws IOException {


        BufferedReader inputStream = null;

        PrintWriter outputStream = null;


        try {/* 来自 时   代     Java  公  众  号 - nowjava.com*/

            inputStream = new BufferedReader(new FileReader("xanadu.txt"));

            outputStream = new PrintWriter(new FileWriter(

                    "characteroutput.txt"));


            String l;

            while ((l = inputStream.readLine()) != null) {

                outputStream.println(l);

            }

        } finally {

            if (inputStream != null) {

                inputStream.close();

            }

            if (outputStream != null) {

                outputStream.close();

            }

        }

    }

}


展开阅读全文