java文件处理--按字符流复制文件( File+FileReader + BufferedReader +FileWriter+ BufferedWriter)

import java.io.*;
public class copyFile{
     copyFile(String mainPath, String subPath, String copy_mainPath, String copy_subPath)  {
        try {
        File initFile = new File(mainPath, subPath);
        FileReader flr = new FileReader(initFile);
        BufferedReader bfr = new BufferedReader(flr);

        File copyFile = new File(copy_mainPath, copy_subPath);
        FileWriter flw = new FileWriter(copyFile);
        BufferedWriter bfw = new BufferedWriter(flw);

        String context;
        while ((context = bfr.readLine()) != null) {//边读入边写出
            bfw.write(context);
            bfw.newLine();
        }

        bfw.close();
        bfr.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public  static  void main(String args[])  {
        String mainPath, subPath, copy_mainPath, copy_subPat;
        mainPath="C:\\Users\\Lenovo\\Desktop";subPath="有趣练习.txt";
        copy_mainPath= mainPath; copy_subPat="有趣练习(copy).txt";
       new copyFile(mainPath, subPath, copy_mainPath, copy_subPat);
    }
}

发布了156 篇原创文章 · 获赞 16 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44001521/article/details/104154847
今日推荐