使用PrintWriter和BufferedReader完成文件拷贝工作

package firstExam.fourth;

import java.io.*;

/**
* 使用PrintWriter和BufferedReader完成文件拷贝工作
*/
public class SourceFile {

public static void main(String[] args) {
    /*
     * File类包含许多获取文件属性的方法,以及重命名和删除文件和目录的方法,
     * 但是File类不包含读写文件内容的方法。
     * 可以使用new File("CopyFile.java");为在当前目录下(工程的一级子目录)
     * 的文件CopyFile.java创建一个File类对象,前提是CopyFile.java文件存在,
     * 创建对象的目的是为了获取File类的一些属性和方法
     */

    /*
     * 所有的输入流都需要在文件已经存在的情况下为该文件创建对象,
     * 而输出流则没有那么严格,如果创建输出流对象的时候,文件不存在,则会自动创建一个
     */
    File file=new File("src\\firstExam\\fourth\\SourceFile.java");
    System.out.println("is file exit?"+file.exists());
    System.out.println("is file?"+file.isFile());
    copy(file);
}

public static void copy(File file){
    /*
     * BufferedReader:从字符输入流中读取文本,缓冲各个字符,
     * 从而提供字符、数组和行的高效读取。
     */     
    BufferedReader bReader=null;
    /*
     * PrintWriter类可用来创建一个文件并向文本文件写入数据,
     * new PrintWriter("mycopy.java");中,如果文件mycopy.java不存在,
     * 调用PrintWriter的构造方法会创建一个新文件。如果文件已经存在,那么文件的当前内容
     * 将在不和用户确认的情况下被废弃。
     */
    PrintWriter pWriter=null;
    try{

        /*
         * FileInputStream可以从一个File对象创建一个FileInputStream,
         * 也可以从一个文件名(字符串)创建一个FileInputStream。
         * 但是如果试图为一个不存在的文件创建FileInputStream,将会发生
         * java.io.FileNotFoundException
         */ 
        /*
         * BufferedReader类的构造方法只能接收Reader(抽象类)类的子类对象,
         * Reads text from a character-input stream,
         * buffering characters so as to provide for 
         * the efficient reading of characters, arrays, and lines.
         */
         /*
          * An InputStreamReader is a bridge from byte streams
          * to character streams: It reads bytes and decodes
          * them into characters using a specified 
          * InputStreamReader的构造方法只能接收InputStream(抽象类)类的子类对象
          */

        bReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        pWriter=new PrintWriter("TargetFile.java");
        String str="";
        /*
         * bReader.readLine()方法:读取一个文本行。通过下列字符之一即可认为某行已终止:
         * 换行('\n')、回车('\r'),或回车后直接跟着换行。
         * 返回:包含该行内容的字符串,不包含任何终止符,如果已到达流末尾,则返回null
         */
        /**
         * Reads a line of text.  A line is considered to be terminated(终止) by any one
         * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
         * followed immediately by a linefeed.
         *
         * @return     A String containing the contents of the line, not including
         *             any line-termination characters, or null if the end of the
         *             stream has been reached
         *
         * @exception  IOException  If an I/O error occurs
         */
        while((str=bReader.readLine())!=null){
            /*
             * 能按行输出的奥秘就在于println,如果换成print,则整个文本内容就会变成一行,
             * 因为我们在编写SourceFile文件时每一行结束都会有一个换行,因此readLine方法可以
             * 确保每一次都顺利结合结束
             */
            pWriter.println(str);
            //清空缓存,及时释放内存
            pWriter.flush();
        }
    //FileNotFoundException继承自IOException   
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();

    }finally{
        if(bReader!=null){
            try {
                bReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(pWriter!=null){
            pWriter.close();
        }
    }

}

}

猜你喜欢

转载自blog.csdn.net/qq_38986609/article/details/78488588