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

类似于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("-------------结束-----------");}

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

PrintStream和PrintWriter相关推荐

  1. IO流(五)__文件的递归、Properties、打印流PrintStream与PrintWriter、序列流SequenceInputStream...

    一.文件的遍历 1.需求:对指定目录进行所有的内容的列出(包含子目录的内容)-文件的深度遍历 思想:递归的思想,在递归的时候要记住递归的层次. public class FileTest {publi ...

  2. 【发现问题】Java中PrintStream和PrintWriter的区别

    PrintStream和PrintWriter的API几乎相同,都能输出各种形式的数据,构造方法也几乎相同.两者有何区别? **2个类的功能基本相同,PrintStream能做的PrintWriter ...

  3. 打印流PrintStream,PrintWriter

    1.打印流: 作用:打印流可以实现方便.高效的打印数据到文件中去.打印流一般是指:PrintStream,PrintWriter两个类. 可以实现打印什么数据就是什么数据,例如打印整数97写出去就是9 ...

  4. 打印流-PrintStream和PrintWriter

    IO包中的其他功能流对象 功能流对象 特点:解决问题的特有方法. 打印流--必须是输出流(两个都是) PrintStream(字节流) PrintWriter(字符流) 特点:打印.不抛异常 打印的目 ...

  5. 黑马毕向东Java课程笔记(day20-1——20-17)IO流:File类及相关方法、递归、递归的相关练习、Properties、PrintWriter类与PrintStream类、合并流与切割流

    1.File类概述   File是文件和目录路径名的抽象表示形式. 用来将文件或者文件夹封装成对象,方便对文件与文件夹的属性信息进行操作.   前面说到的"流",它只能操作数据,想 ...

  6. Java打印流(PrintStream/PrintWriter)

    概念 打印流是输出信息最方便的类,注意包含字节打印流PrintStream和字符打印流PrintWriter.打印流提供了非常方便的打印功能,可以打印任何类型的数据信息,例如:小数,整数,字符串.而且 ...

  7. java printwriter format_Java的格式化输出

    在JavaSe5中,推出了C语言中printf()风格的格式化输出.这不仅使得控制输出的代码更加简单,同时也给与Java开发者对于输出格式与排列更大的控制能力.今天,我们开始学习Java中的格式化输出 ...

  8. java学习笔记------ PrintStream

    PrintStream,它可以将Java的基本资料型态等资料,直接转换为系统预设编码下对应的字元,再输出至OutputStream中,而这边要介绍的 PrintWriter其功能上与PrintStre ...

  9. 打印流PrintStream和配置流Propeties

    打印流PrintStream和PrintWriter /*** 打印流PrintStream*/@Testpublic void Print() throws IOException {PrintSt ...

最新文章

  1. AIDL注意细节 简单Demo
  2. 七、Sqoop架构,安装和基本使用
  3. 树莓派AI视觉云台——8、WiringPi库函数
  4. c语言函数的程序设计,C语言程序设计第3版,第6章函数程序设计.ppt
  5. Android IPC机制
  6. Wireshark抓包常见问题解析(转)
  7. ubuntu wps缺少字体_WPS各版本
  8. HDU 1000 A + B Problem
  9. python 生成html文件浏览器_Handout库:能将python脚本转化为html展示文件
  10. 为什么用自己的电脑部署服务器之后外网访问不到呢?并且该如何解决这个问题呢?...
  11. 【渝粤教育】国家开放大学2018年秋季 0714-21T建筑识图与CAD 参考试题
  12. 保障系统迁移服务器搬迁,整体中心机房搬迁服务器idc数据迁移方案应用场景
  13. java开发一年后学习计划
  14. python sub函数用法_Python Pandas dataframe.sub()用法及代码示例
  15. SD/SDHC/SDXC区别
  16. 苹果终端date命令_mac终端常用命令
  17. 基于图像的三维重建——对极几何(3)
  18. 常见的12个深度学习面试问题(提高篇)
  19. 软件工程毕业设计题目安卓Android移动端推荐
  20. 最全SQL数据库语法总结

热门文章

  1. 自定义vue SSR
  2. Keil C51与STC-ISP安装
  3. 浅谈直流电机并联瓷片电容的作用
  4. 商业化广告--体系学习-- 2 -- 行业蓝图篇 -- 广告产品与商业模式
  5. nvidia官方cuda性能测试软件,NVIDIA CUDA应用软件测试
  6. python 量子计算包_ProjectQ:解锁Python实现量子计算的新方式!
  7. PHP cURL学习-域名备案查询
  8. 最常用计算机机箱,电脑机箱哪款比较好 推荐实用的四款机箱
  9. [六秋01]宋元诗歌赏读
  10. 备份和导入Outlook 2016 电子邮件签名