其他流

一、节点流
1.字节数组 节点流
输入流 ByteArrayInputStream-----read(byte[] b,int off,int len)+close()
输入流 ByteArrayOutputStream----write(byte[] b,int off,int len)+toByteArray() 不能使用多态

package otherio;
/** 156 字节数组流的读写* */
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ByteArrayDemo1 {public static void main(String[] args) throws IOException {read(write());}//输入流操作与文件输入流操作一致public static void read(byte[] src) throws IOException {//数据源//选择流InputStream is1=new BufferedInputStream(new ByteArrayInputStream(src));//操作byte[] car=new byte[1024];int len=0;while (-1!=(len=is1.read(car))) {System.out.println(new String(car,0,len));}//is1.close();}//输出流操作与文件输出流操作有些不同,有新增方法,不能使用多态public static byte[] write() throws IOException{//目的地byte[] dest;//选择流 不同ByteArrayOutputStream b1=new ByteArrayOutputStream();//操作 写出String s1="输出流操作与文件输出流操作有些不同,有新增方法,不能使用多态";byte[] info =s1.getBytes();b1.write(info);//获取数据dest=b1.toByteArray();//b1.close();return dest;}}

package otherio;
/** 156 字节数组流与文件流的对接* 文件--程序--字节数组*   a.文件输入流*   b.字节数组输出流*  字节数组 --程序--文件*   a.字节数组输入流*   b.文件输出流* */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ByteArrayDemo2 {public static void main(String[] args) throws IOException {byte[] test=getBytesFromFile("H:/1/1.jpg");System.out.println(new String(test));toFileFromByteArray(test, "H:/1/2.jpg");//也可以是其他格式}public static byte[] getBytesFromFile(String srcPath) throws IOException {//创建源文件和目的地File f1=new File(srcPath);byte[] dest=null;//选择流InputStream is1=new BufferedInputStream(new FileInputStream(f1)) ;ByteArrayOutputStream bos1=new ByteArrayOutputStream();//操作  不断读取文件,写出到字节数组流中byte[] car=new byte[1024];int len=0;while (-1!=(len=is1.read(car))) {//写出到数组流中bos1.write(car,0,len);}bos1.flush();//获取数据dest=bos1.toByteArray();bos1.close();is1.close();return dest;}public static byte[] toFileFromByteArray(byte[] src,String destPath) throws IOException {//创建源//目的地File dest=new File(destPath);//选择流InputStream is1=new BufferedInputStream(new ByteArrayInputStream(src));OutputStream os1=new BufferedOutputStream(new FileOutputStream(dest));操作  不断读取数组,写出到文件输出流中byte[] car=new byte[1024];@SuppressWarnings("unused")int len=0;while(-1!=(len=is1.read(car))){os1.write(car,0, car.length);}os1.flush();os1.close();is1.close();return null;}
}

二、数据流
1.基本类型+String 保留数据+类型
输入流:DataInputStream readxxx
输出流:DataOutputStream writexxx

package otherio;
//158 基本类型(基本+String)处理流
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class DataDemo1 {public static void main(String[] args) throws IOException {write("H:/1/1.txt");read("H:/1/1.txt");}//数据+类型输出到文件public static void write(String destPath) throws IOException {double point=2.5;long num=100L;String s1="数据类型";//创建源File dest=new File(destPath);//选择流 DataOutputStreamDataOutputStream dos1=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));//操作 写出的顺序为读取准备dos1.writeDouble(point);dos1.writeLong(num);dos1.writeUTF(s1);dos1.flush();dos1.close();}//从文件中读取数据+类型@SuppressWarnings("resource")public static void read(String destPath) throws IOException {File src=new File(destPath);//选择流 DataInputStreamDataInputStream dis1=new DataInputStream(new BufferedInputStream(new FileInputStream(src)));//操作 读取顺序必须与写出一致double num1=dis1.readDouble();Long num2=dis1.readLong();String s2=dis1.readUTF();System.out.println(num1);System.out.println(num2);System.out.println(s2);}
}


三、引用类型(对象) 保留数据+类型
反序列化 输入流:ObjectInputStream readObjec()
序列化 输出流:ObjecOutputStream writeObjec()
注意:1.先序列化,再反序列化。反序列化顺序必须与序列化一致。
2.不是所有的对象都可以序列化,java.io.Serializable
不是所有的属性都需要序列化 transient

package otherio;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.Arrays;
//159
public class EmployeeDemo1 {public static void main(String[] args) throws IOException, ClassNotFoundException {// TODO Auto-generated method stubseri("H:/1/1.txt");read("H:/1/1.txt");}//序列化public static void seri(String destPath) throws IOException{Employee e1=new Employee("lzk", 600);File dest=new File(destPath);int[] arr1={1,2,3,4,5};//数组也可以序列化//选择流 ObjectOutputStreamObjectOutputStream dos1=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));//写入流dos1.writeObject(e1);dos1.writeObject(arr1);dos1.flush();dos1.close();}//反序列化public static void read(String destPath) throws IOException, ClassNotFoundException{File src=new File(destPath);//选择流 ObjectInputStream dis1=new ObjectInputStream(new BufferedInputStream(new FileInputStream(src)));//操作  Object o1=dis1.readObject();if (o1 instanceof Employee) {Employee e1=(Employee)o1;System.out.println(e1.getName());System.out.println(e1.getSalary());}Object o2=dis1.readObject();int[] arr1=(int[])o2;System.out.println(Arrays.toString(arr1));dis1.close();}
}
package otherio;
//159
public class Employee implements java.io.Serializable {private static final long serialVersionUID = 1L;private transient String name;private double salary;public Employee(String name, double salary) {super();this.name = name;this.salary = salary;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}
}


四:打印流
PrintStream(属于处理流)

package otherio;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
//161
public class PrintStreamDemo01 {public static void main(String[] args) throws FileNotFoundException {// TODO Auto-generated method stubPrintStream ps1=System.out;ps1.println(false);//输出到文件File src=new File("H:/1/1.txt");ps1=new PrintStream(new BufferedOutputStream(new FileOutputStream(src)));ps1.print("IO12346");ps1.close();}}

System的三个常量及重定向

package otherio;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
/** 161 三个常量* 1.System.in 输入流   键盘,文件输入* 2.System.out 输出流  控制台输出* 3.System.err 输出流  控制台输出* * 重定向:* setIn()* setOut()* setErr()* * FileDescriptor.in* FileDescriptor.out* FileDescriptor.err* */
public class SystemDemo1 {public static void main(String[] args) throws FileNotFoundException {// TODO Auto-generated method stubtest2();//重定向  控制台---》文件System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("H:1/1.txt")),true));System.out.println("重定向到文件");//重定向  文件---》控制台System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));System.out.println("重定向到控制台");}public static void test2() throws FileNotFoundException{InputStream is1=System.in;//从文件中输入,而不是键盘 (is1已经不再是System.in,而是FileInputStream)is1=new BufferedInputStream(new FileInputStream("H:/1/1.txt"));Scanner sc1=new Scanner(is1);System.out.println(sc1.nextLine());}}

IO设计模式

package patterio;
/** 162 IO里的设计模式:装饰设计模式* 类与类之间的关系:* 1.依赖:形参或者局部变量* 2.关联:属性(本例中Amplifier类中的voice对象就被看为属性)*     聚合:属性 整体与部分 不一致的生命周期*     组合:属性 整体与部分 一致的生命周期* 3.继承:父子类关系* 4.实现:接口与实现类的关系* */
public class App {public static void main(String[] args) {Voice v1=new Voice();v1.say();Amplifier a1=new Amplifier(v1);a1.say();}
}
package patterio;
//162
public class Voice {private int voice=10;public Voice() {}public Voice(int voice) {super();this.voice = voice;}public int getVoice() {return voice;}public void setVoice(int voice) {this.voice = voice;}public void say() {System.out.println("音量:"+voice);}
}
package patterio;
//162
public class Amplifier {private Voice voice;public Amplifier() {// TODO Auto-generated constructor stub}public Amplifier(Voice voice) {super();this.voice = voice;}public void say() {System.out.println("音量:"+voice.getVoice()*100);;}}

DAY20:尚学堂高琪JAVA(156~164)其他流及 IO的设计模式相关推荐

  1. DAY12:尚学堂高琪JAVA(119~123)Hashmap 与冒泡排序

    Hashmap 与冒泡排序 map,set,list的区别 添加链接描述 添加链接描述 添加链接描述 MyArrayList.java package fanxing; import java.uti ...

  2. 【160天】尚学堂高琪Java300集视频精华笔记(129)

    明天开始,专栏增加一个黑马程序员的课程更新. 其它容器收尾讲解 队列Queue与Deque(单向队列与双向队列) Enumeration(较老的接口,JDK1.5前使用频繁,维护旧系统会用到) Has ...

  3. 尚学堂 高琪JAVA300集第十一章作业 编程题答案

    本人 JAVA初学者 在寻找这一方面的答案时没有看见 ,本着分享的精神 自己做了出来 也就传上来了 水平有限 存在有错的地方或者改进的方法 ,望大佬们可以提出 万分感谢. 1.1. 设计一个多线程的程 ...

  4. 【133天】尚学堂高淇Java300集视频精华笔记(71-72)

    第71集:常用类/file类/打印目录树状结构/递归算法 课堂代码 package com.test071;import java.io.File;public class Test071 {publ ...

  5. python 递归函数_Python尚学堂高淇|P82P86面向对象和面向过程的区别LEGB规则nonlocal_global递归函数阶乘计算案例...

    P82-递归函数-阶乘计算案例 #使用递归函数计算阶乘def factorial(n):if n==1:return 1else:return n*factorial(n-1)result=facto ...

  6. 【138天】尚学堂高淇Java300集视频精华笔记(84)

    第84集:太阳系模型/基本类的封装/Star类的建立 本集知识点 将对象尽可能的抽象,可以有效减少代码量,比如此例中的Star类 package com.test084_087_solar;impor ...

  7. python释放变量内存_Python尚学堂高淇|1113引用的本质栈内存,堆内存,内存的示意图,标识符,变量的声明初始化,垃圾回收机制...

    011-引用的本质-栈内存和堆内存-内存的示意图在Python当中,变量也成为:对象的引用,因为,变量的存储就是对象的地址变量通过地址引用了对象变量位于堆内存(压栈,出栈等细节,后续再介绍)对象位于: ...

  8. 【135天】尚学堂高淇Java300集视频精华笔记(74-76)

    第74-75集:异常机制.trycatchfinallyreturn执行顺序.捕获异常.声明异常throw.方法重写中异常的处理.手动抛出异常 异常的处理办法一:捕获异常 try try语句指定了一段 ...

  9. 【131天】尚学堂高淇Java300集视频精华笔记(65-66)

    第65集:常用类Date类的使用JDk源码分析 Date时间类(java.util.Date) 在标准Java类库中包含一个Date类.它的对象表示一个特定的瞬间,精确到毫秒. Date()分配一个D ...

  10. vb.net 同时给多个属性赋值_Python尚学堂高淇|1721时间表示unix时间点毫秒微秒time模块浮点数自动转换强制转换增强赋值运算符...

    017浮点数-自动转换-强制转换-增强赋值运算符浮点数称为float用a*b^10形式表示的科学计数法,比如:3.14,314E-2或者314e-2这些数字在内存当中也是按照科学计数法存储. > ...

最新文章

  1. R-CNN系列学习笔记
  2. 【实施工程师】Linux查看日志后100行
  3. servlet源码查看
  4. 小红书消息中间件的运维实践与治理之路
  5. 关于vue中sync修饰符的用法
  6. 将语音识别准确率提升40% 他是当下最受比尔·盖茨器重的中国人
  7. Solaris11操作命令汇总
  8. ArcFace 论文阅读及 pytorch 实现
  9. QQ聊天记录生成词云--WordCloud
  10. ubuntu 7z解压
  11. svchost 总是占用网速 一招见效(实测)
  12. java单例模式(下)
  13. html图片excel路径,jsp方式导出Excel能不能导出图片_html/css_WEB-ITnose
  14. 当你想吃夜宵的时候你会到谁
  15. MySQL中show命令用法大全
  16. Mac与Linux的文件系统,Apple为iOS和macOS提供全新文件系统APFS
  17. #HHD32F107# AD采集
  18. 基于Python,OpenCV,Numpy和Albumentations实现目标检测的合成数据集
  19. Docker部署RabbitMQ
  20. 索尼xzp升级android p,索尼XZP国行版升级安卓8.0 相机功能优化

热门文章

  1. echarts甘特图
  2. microusb贴片 ad封装_diy从pcb到焊接,到程序调试,真正意思上的diy机械键盘pcb由ad绘制...
  3. Android,保存图片并通知系统更新相册
  4. css完美参考手册 chm,css4.0参考手册
  5. JVM内存分配担保机制
  6. 指尖轻舞桌面:Slide On Desk - 主题风格制作指南
  7. 股票市场行情走势图绘制
  8. 网站banner写法
  9. 什么是 Sidecar
  10. 常用下载方式的区别-BT下载、磁力链接、电驴