前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java-字符流逐行读取/写入文件

Java-字符流逐行读取/写入文件

作者头像
itze
发布2022-10-31 15:54:44
1.3K0
发布2022-10-31 15:54:44
举报
文章被收录于专栏:IT者
代码语言:javascript
复制
    /**
     * 字符流,每次读取/写入一行
     * BufferedReader/PrintWriter
     * @Author: www.itze.cn
     * @Date: 2020/9/28 10:29
     * @Email: 814565718@qq.com
     * @param srcFile
     * @param destFile
     */
    public static void brAndpw(String srcFile,String destFile){
        try {
            BufferedReader reader = new BufferedReader(new FileReader(srcFile));
            //参数说明,第二个true时,写入的时候会自动刷新,就不需要刷新了
            PrintWriter writer = new PrintWriter(new FileOutputStream(destFile), true);
            String str = null;
            //读取,每次读取一行
            while ((str=reader.readLine())!=null){
                //写入,println:写入并自动换行,print:写入但是不执行换行
                writer.println(str);
            }
            reader.close();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

使用方法

代码语言:javascript
复制
    public static void main(String[] args) {
        String srcFile = "D:\\newCountry.txt";
        String destFile = "D:\\newCountry2.txt";
        brAndpw(srcFile,destFile);
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020年9月28日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档