PrintStream和PrintWriter

本文探讨了字节打印流和字符打印流在文件写入操作中的效率,指出两者都提供了高效的输出,尤其是PrintWriter由于其内部的缓冲机制,使用起来既简单又快速。通过示例代码展示了如何使用BufferedWriter和PrintWriter将字符串写入文本文件,并比较了它们的执行时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这两个都是打印流,第一个是字节打印流,第二个是字符打印流。都是用来将数据写到文本文件的。

类似于OutputSteam、BufferWriter、Writer

但是这个会更高效和简洁,因为里面封装了BufferWriter方法,所以在速度方面不比字符缓冲流慢,同时比字符缓冲流更简介

使用字符缓冲流将字符串的数据写到word.txt文件里:

public class a {
    public static final Logger log = LoggerFactory.getLogger("test_01.class");
    public static void main(String[] args) throws IOException {

        // 需要写到文本文件中的字符串

        String str = null;
        if (true) {
            str = "atch the star that holds your destiny, the one that forever twinkles within your heart. Take advantage of " +
                    "precious opportunities while they still sparkle before you. Always believe that your ultimate goal is attainable as long as you commit yourself to it.\n" +
                    "追随能够改变你命运的那颗星,那颗永远在你心中闪烁的明星。当它在你面前闪耀时,抓住这宝贵的机会。请谨记,只要你坚持不懈,最终的目标总能实现。\n" +
                    "Though barriers may sometimes stand in the way of your dreams, remember that your destiny is hiding behind them. " +
                    "Accept the fact that not everyone is going to approve of the choices you've made, " +
                    "have faith in your judgment, catch the star that twinkles in your heart, and it will " +
                    "lead you to your destiny's path. Follow that pathway and uncover the sweet sunrises that await you.\n" +
                    "尽管实现梦想的途中有时会遇到障碍,要知道这是命运对你的挑战。不是每个人都会赞成你的选择,接受这个现实,并相信自我的判断,追随那颗在你心中闪烁的明星," +
                    "它会引领你踏上命运的征途。坚持不懈,你就能享受那些幸福时刻。";
        }

        BufferedWriter bos = new BufferedWriter(new FileWriter("word.txt"));

        long start_time = System.currentTimeMillis();
        log.info("-------------开始-----------");

        // 将str字符串的内容写到文本文件里
        bos.write(str,0,str.length());
        bos.close();
        long end_time = System.currentTimeMillis();
        log.info("共花了" + (end_time - start_time)/1000.0 + "毫秒" );
        log.info("-------------结束-----------");

    }
}

运行结果:

使用PrintWriter流将数据写到文本文件中:

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

        // 需要写到文本文件中的字符串

        String str = null;
        if (true) {
            str = "atch the star that holds your destiny, the one that forever twinkles within your heart. Take advantage of " +
                    "precious opportunities while they still sparkle before you. Always believe that your ultimate goal is attainable as long as you commit yourself to it.\n" +
                    "追随能够改变你命运的那颗星,那颗永远在你心中闪烁的明星。当它在你面前闪耀时,抓住这宝贵的机会。请谨记,只要你坚持不懈,最终的目标总能实现。\n" +
                    "Though barriers may sometimes stand in the way of your dreams, remember that your destiny is hiding behind them. " +
                    "Accept the fact that not everyone is going to approve of the choices you've made, " +
                    "have faith in your judgment, catch the star that twinkles in your heart, and it will " +
                    "lead you to your destiny's path. Follow that pathway and uncover the sweet sunrises that await you.\n" +
                    "尽管实现梦想的途中有时会遇到障碍,要知道这是命运对你的挑战。不是每个人都会赞成你的选择,接受这个现实,并相信自我的判断,追随那颗在你心中闪烁的明星," +
                    "它会引领你踏上命运的征途。坚持不懈,你就能享受那些幸福时刻。";
        }

        PrintWriter bos = new PrintWriter(new FileWriter("word.txt"));
//        PrintWriter bos = new PrintWriter("word.txt");            可以直接写文件路径

        long start_time = System.currentTimeMillis();
        log.info("-------------开始-----------");

        // 将str字符串的内容写到文本文件里
        bos.write(str,0,str.length());
        bos.close();
        long end_time = System.currentTimeMillis();
        log.info("共花了" + (end_time - start_time)/1000.0 + "毫秒" );
        log.info("-------------结束-----------");

    }

 

 

 

对于字符缓冲流,速度和打印流一样,对于字节缓冲流来说,字节打印流速度会更快而且更高效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值