我们之前所做的都是对文件进行IO处理,实则我们也可以对内存进行IO处理。我们将发生在内存中的IO处理称为内存流。

内存操作流也可分为两类:字节内存流和字符内存流。

(1)ByteArrayInputStream  和  ByteArrayOutputStream

构造函数:

public class ByteArrayOutputStream extends OutputStream

public class ByteArrayInputStream extends InputStream

(2)CharArrayInputStream  和  CharArrayOutputStream

构造函数:

public class CharArrayWriter extends Writer

public class CharArrayReader extends Reader

由此我们可以得出内存流的继承关系:

利用内存流实现大小写转换:


  1. package myIO;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. /*通过内存流实现大小写转换
  8. * */
  9. public class Test3 {
  10. public static void main(String[] args) throws IOException {
  11. String string = "hello world,hello lemon";
  12. //实例化InputStream对象,进行实例化时应将需要操作的数据保存在内存中,读取的将是设置的内容
  13. InputStream inputStream = new ByteArrayInputStream(string.getBytes());
  14. //实例化OutputStream对象
  15. OutputStream outputStream = new ByteArrayOutputStream();
  16. int temp= 0;
  17. //判断是否读完
  18. while((temp = inputStream.read())!=-1) {
  19. //对每个字节进行处理,处理之后存在于OutputStream类中
  20. //进行大小写转换
  21. outputStream.write(Character.toUpperCase(temp));
  22. }
  23. //直接对outputStream对象进行输出
  24. System.out.println(outputStream);
  25. //关闭流
  26. inputStream.close();
  27. outputStream.close();
  28. }
  29. }

(2)内存流的操作

内存流有个很小的功能就是实现两个文件的合并操作(对很小的文件而言)。

内存流最核心的就是:将所有OutputStream输出的程序保存在了程序中,所以可以利用这一特征来进行处理。


  1. /*
  2. * 实现两个文件的合并处理
  3. * */
  4. public class Test3{
  5. public static void main(String[] args) throws IOException {
  6. String singal = File.separator;
  7. //指明要合并两个文件的地址
  8. File[] files = new File[] {
  9. new File("C:"+singal+"Users"+singal+"lenovo"+singal+"Desktop"+singal+"Test1.txt")
  10. ,new File("C:"+singal+"Users"+singal+"lenovo"+singal+"Desktop"+singal+"Test2.txt")};
  11. String[] data = new String[2];
  12. for(int i = 0 ;i <files.length;i++) {
  13. data[i] = readFile(files[i]);
  14. }
  15. StringBuffer stringBuffer = new StringBuffer();
  16. String contentA [] = data[0].split(" ") ;
  17. String contentB [] = data[1].split(" ") ;
  18. for (int i = 0; i < contentA.length ; i++) {
  19. stringBuffer.append(contentA[i]).append("(").append(contentB[i]).append(")").append(" ") ;
  20. }
  21. System.out.println(stringBuffer);
  22. }
  23. //读取文件内容,使用File对象因为其包含完整的路径信息
  24. public static String readFile(File file) throws IOException {
  25. if(file.exists()) {
  26. InputStream inputStream = new FileInputStream(file);
  27. //没有向上转型,因为后续要使用toByteArray()方法
  28. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  29. int temp = 0;
  30. byte[] data = new byte[10];
  31. while((temp = inputStream.read(data))!=-1) {
  32. //将数据保存在byteArrayOutputStream
  33. byteArrayOutputStream.write(data,0,temp);
  34. }
  35. byteArrayOutputStream.close();
  36. inputStream.close();
  37. return new String(byteArrayOutputStream.toByteArray());
  38. }
  39. return null;
  40. }
  41. }

2、打印流

打印流的本质是解决OutputStream的缺陷,因为若想将非二进制的数据通过程序向终端目标直接输出信息的话,OutputStream有如下缺陷:所有的数据都要转换成字节数组,若要输出int、double更是不方便。

(1)进行简单打印流的设计


  1. /*
  2. * 设计简单打印流
  3. * */
  4. class PrintDesign{
  5. private OutputStream outputStream;
  6. //构造函数
  7. //有外部传入要输出的目的终端
  8. public PrintDesign(OutputStream outputStream) {
  9. super();
  10. this.outputStream = outputStream;
  11. }
  12. //实现输出的功能(传入参数为字符串)
  13. public void print(String string) throws IOException {
  14. this.outputStream.write(string.getBytes());
  15. }
  16. //实现换行功能
  17. public void println(String string) throws IOException {
  18. this.print(string+"\r\n");
  19. }
  20. //传入参数为int
  21. public void print(int data) throws IOException {
  22. this.print(String.valueOf(data));
  23. }
  24. public void println(int data) throws IOException {
  25. this.print(String.valueOf(data)+"\r\n");
  26. }
  27. }
  28. public class Test3{
  29. public static void main(String[] args) throws IOException {
  30. String singal = File.separator;
  31. PrintDesign printDesign = new PrintDesign(new FileOutputStream(
  32. new File("C:"+singal+"Users"+singal+"lenovo"+singal+"Desktop"+singal+"Test1.txt")));
  33. printDesign.println("姓名:");
  34. printDesign.print("lemon");
  35. printDesign.println("年龄:");
  36. printDesign.print(20);
  37. }
  38. }

我们可以发现经过简单的处理使得OutputStream的功能变得更加强大了,其实我们所做的工作只不过是对OutputStream做了一个封装而已。实则java中提供有专门的打印流处理类:字节打印流rintStream、字符打印流PrintWriter.

(2)认识打印流处理类

字节打印流:public class PrintStream extends FilterOutputStream

构造函数:public PrintStream(OutputStream out)

字符打印流:public class PrintWriter extends Writer

构造函数:public PrintWriter(OutputStream out)

public PrintWriter (Writer out)

打印流的设计模式属于装饰者设计模式:核心是某个类的功能,但是为了得到更好的操作效果,让其支持的功能更多一些。

(3)打印流的使用

在后续的应用中字符打印流(PrintWriter)引用比较对,因为它可以接收Writer和OutputStream两种类型的参数;而字节打印流(PrintStream)只可接收OutputStream类型的参数。


  1. /*
  2. * 字符打印流的使用
  3. * */
  4. public class Test3{
  5. public static void main(String[] args) throws FileNotFoundException {
  6. String singal = File.separator;
  7. PrintWriter printWriter = new PrintWriter(
  8. new File("C:"+singal+"Users"+singal+"lenovo"+singal+"Desktop"+singal+"Test1.txt"));
  9. printWriter.print("班级:");
  10. printWriter.println("数学152");
  11. printWriter.println("年龄:");
  12. printWriter.println(20);
  13. printWriter.close();
  14. }
  15. }

(4)格式化输出

我们在C语言遇到一个printf()函数,这个函数在输出的时候可以使用一些占位符,例如:字符串(%s)、数字(%d)、小数 (%f)、字符(%c)等。从JDK1.5开始,PrintStream类中也追加了此种操作。

格式化输出:public PrintStream printf(String format, Object ... args)


  1. /*
  2. * 格式化输出
  3. * */
  4. public class Test3{
  5. public static void main(String[] args) throws FileNotFoundException {
  6. String name = "lemon";
  7. int age = 20;
  8. double salary = 10.3;
  9. String singal = File.separator;
  10. PrintStream printStream = new PrintStream(new FileOutputStream(
  11. new File("C:"+singal+"Users"+singal+"lenovo"+singal+"Desktop"+singal+"Test2.txt")));
  12. printStream.printf("姓名:%s,年龄:%d,工资:%f",name,age,salary);
  13. printStream.close();
  14. }
  15. }



JavaIO——内存操作流、打印流的更多相关文章

  1. IO流的Properties集合,序列化流与反序列化流,打印流及commons-IO

    内容介绍 Properties集合 序列化流与反序列化流 打印流 commons-IO Properties类 Properties类介绍 Properties 类表示了一个持久的属性集.Proper ...

  2. Java API —— IO流(数据操作流 & 内存操作流 & 打印流 & 标准输入输出流 & 随机访问流 & 合并流 & 序列化流 & Properties & NIO)

    1.操作基本数据类型的流     1) 操作基本数据类型 · DataInputStream:数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型.应用程序可以使用数据输出 ...

  3. 6.5(java学习笔记)其他流(字节数组流,数据流,对象流,打印流)

    一.字节数组流 之前使用输入输出流的操作的对象是文件,而这里字节数组流操作的对象是内存,内存可以看做是一个字节数组. 使用字节数组流读写就可以看做是从内存A到内存B的读写,对象时内存即字节数组. 1. ...

  4. java 转换流 打印流 数据流

    转换流 InputStreamReader 和 OutputStreamWriter处理流用于将字节流转化成字符流,字符流与字节流之间的桥梁InputStreamReader 的作用是把 InputS ...

  5. Properties-转换流-打印流-序列化和反序列化-Commons-IO工具类

    一.Properties 类(java.util)     概述:Properties 是一个双列集合;Properties 属于map的特殊的孙子类;Properties 类没有泛型,propert ...

  6. JAVA笔记12__字节、字符缓冲流/打印流/对象流/

    /** * !!:以后写流的时候一定要加入缓冲!! * 对文件或其它目标频繁的读写操作,效率低,性能差. * 缓冲流:好处是能更高效地读写信息,原理是将数据先缓冲起来,然后一起写入或读取出来. * * ...

  7. python3 subprocess 内存操作视频转换流格式

    import subprocessout = open('./tmp/sss.mp4','rb').read()p = subprocess.Popen(["./ffmpeg",& ...

  8. 021.10 IO流 打印流

    内容:PrintStream:字节流    PrintWriter:字符流 PrintStream public static void main(String[] args) throws IOEx ...

  9. 34 io流-- 打印流和对象流

    概述 io流分为字符流和字节流,具体分类相见下图 字符流:char 一些基本文本的数据传输 字节流:byte 图片.视频等用文本查看器查看不了的文件都是二进制文件,只能用字节流传输,使用字符流cp的看 ...

随机推荐

  1. IDEA免费激活至2099年教程,亲测可用

    申明,本教程 Intellij IDEA 最新版激活教程,激活码均收集与网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除.如条件允许,建议大家购买正版. 以下是本人免费激活到 2099 年的 ...

  2. docker的集群管理工具

    docker 集群管理三剑客: docker compose: Compose 是用于定义和运行多容器 Docker 应用程序的工具.通过 Compose,您可以使用 YML 文件来配置应用程序需要的 ...

  3. Zabbix 4.4 离线安装 使用mariadb的踩坑,无法停止服务

    先分享一个网站,之前就没注意过有这个网站,不知道是啥时候开放的.里面分享了N多zabbix的模板. https://share.zabbix.com/ 报错如下 Unsupported charset ...

  4. ReplacingMergeTree:实现Clickhouse数据更新

    摘要:Clickhouse作为一个OLAP数据库,它对事务的支持非常有限.本文主要介绍通过ReplacingMergeTree来实现Clickhouse数据的更新.删除. 本文分享自华为云社区< ...

  5. 【浏览器】聊聊DOM

    [浏览器]聊聊DOM 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 作为前端开发,在以前的工作中大多是和DOM打交道,到 ...

  6. 监控框架 - prometheus - 参数指标

    基于SpringBoot2.0+ Actuator metrics的监控(基于Oracle JDK9,G1) 引言 SpringBoot2在spring-boot-actuator中引入了microm ...

  7. 物联网3D,物业基础设施3D运维,使用webgl(three.js)与物联网设备结合案例。搭建智慧楼宇,智慧园区,3D园区、3D物业设施,3D楼宇管理系统——第八课

    写在前面的废话: 很久没有更新文章了,这段时间一直忙于项目落地,虽然很忙,但是感觉没有总结,没有提炼的日子,总是让人感觉飘飘忽忽的. 所幸放下一些事,抽出一些时间,把近期的项目做一些整理与记录.也算是 ...

  8. 【Sass/SCSS】预加载器中的“轩辕剑”

    [Sass/SCSS]预加载器中的"轩辕剑" 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 随着前端 ...

  9. <C#任务导引教程>练习六

    //54五名学生参加了两门课程的考试,试输入成绩,统计出各科的最高分,最低分,平均分及每个学生的平均成绩(要求用对话框显示成绩统计结果)using System;using System.Window ...

  10. Chrome 插件特性及实战场景案例分析

    一.前言 提起Chrome扩展插件(Chrome Extension),每个人的浏览器中或多或少都安装了几个插件,像一键翻译.广告屏蔽.录屏等等,通过使用这些插件,可以有效的提高我们的工作效率:但有时 ...