一、IO与流

以程序为主体,input(输入)是读、获取、使用输入流的过程,output(输出)是写、发送的过程。

JAVA IO 标准的输入与输出

JAVA IO将读和写按照方向划分为:

  • 输入:用于读取
  • 输出:用于写出
  • 区分方向的参照点是程序,从外界获取信息到程序中的方向是输入,反之是输出。
  • JAVA IO以标准化的方式对外界设备进行读写操作。使得我们读写外界设备的方式
  • 基本是统一的。

输入流与输出流

  • 输入流:java.io.InputStream,它是所有字节输入流的超类,定义了所有字节输入 流都必须具备的读取数据的方法。
  • 输出流:java.io.OutputStream,所有字节输出流的超类,定义了写出操作。
  • 流的读写形式是顺序读写模式,即:读写数据只能顺序向后进行,是不能回退的。

节点流与处理流

  • 流除了按照读写方向分类外,还有一种分类就是:节点流与处理流
  • 节点流:数据源明确,是真实连接程序与另一端的"管道",负责实际读写数据的流读写一定是建立在节点流的基础上进行的。它在行业里也称为:低级流
  • 处理流:不能独立存在,必须连接在其他流上,目的是当数据流经当前流时,可以对数据进行加工处理,简化我们的操作,行业里也称为:高级流

文件流

  • java.io.FileInputStream和FileOutputStream
    文件流是一对低级流,用于读写文件的流。功能上与RandomAccessFile一致, 但是文件流是基于JAVA标准的IO,读写是分开的,并且流的读写是顺序读写, 只能顺序向后进行读或写操作,是不能回退的。
    但是RAF是基于指针的随机读写形式,可以调整指针位置对文件任意位置读写。

二、文件流(输出流,向程序内写入数据)

文件流的两种模式
* 1.覆盖模式
* FileOutputStream(String path)
* FileOutputStream(File file)
* 覆盖模式:在写下构造方法时,如果指定的文件中存在数据 则删除当前文件中的所有数据后进行操作
*
* 2.追加模式,对应的构造方法:
* FileOutputStream(String path,boolean append)
* FileOutputStream(File file,boolean append)
* 追加模式:当第二个参数为true,创建文件流时,如果指定文件 已经存在,则通过追加当前流写入的内容都会被顺序追加到文件末尾

//1 FileOutputStream fos = new FileOutputStream("./fos.txt");
FileOutputStream fos = new FileOutputStream("./fos.txt",true);
//2
//3File file = new File("./fos.txt");
//FileOutputStream fos = new FileOutputStream(file);/*1and3 String ling = "asdasdasda123@#";byte[]data = ling.getBytes("utf-8");fos.write(data);*/String ling = "asdasdasda123@#";byte[]data = ling.getBytes("utf-8");fos.write(data);//2System.out.println("over");fos.close();

三、输入流(从文件中读取数据)

文件输入流,用于从文件中读取字节

FileInputStream fis = new FileInputStream("./fos.txt");byte[]data = new byte[200];int len = fis.read(data);System.out.println("实际读取到了"+len+"个字节");String str = new String(data,0,len,"utf-8");System.out.println(str);fis.close();

四、缓存流(属于处理流)

java.io.BufferedInputStream和BufferedOutputstream

  • 缓冲流是一种高级流,在流的连接中作用为加快读写效率。无论我们使用块读写还是单字节读写,缓冲流最终都会统一转换为块读写以保证 读写效率
package io;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class CopyDemo2 {public static void main(String[] args) throws IOException {FileInputStream fis = new FileInputStream("image.png");BufferedInputStream bis = new BufferedInputStream(fis);FileOutputStream fos = new FileOutputStream("image77.png");BufferedOutputStream bos = new BufferedOutputStream(fos);int d = 0;long start = System.currentTimeMillis();while((d=bis.read())!=-1){bos.write(d);}long end = System.currentTimeMillis();System.out.println("copy over!usetime:"+(end-start));bis.close();bos.close();}}

缓冲字节输出流写数据的缓冲区问题

FileOutputStream fos = new FileOutputStream("raf.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);String str = "1231231231asdasddas";byte[]data = str.getBytes("utf-8");bos.write(data);/*缓冲流在没有装够8kb数据时不运行,这个时候就需要我们引用* void flush()* 强制将缓冲流的缓冲区中已经缓存的数据写出*/bos.flush();System.out.println("over");bos.close();

五、对象流

java.io.ObjectInputStream和ObjectOutputStream
对象流是一对高级流,作用是方便读写任何java对象

package io;import java.io.Serializable;
import java.util.Arrays;/*** 使用当前类测试对象流的对象读写操作* @author 毛**/
public class Person implements Serializable{/*** 序列号*/private static final long serialVersionUID = 1L;
private String name;
private int age;
private String gender;
private transient String[]otherInfo;
/** transient关键字* 当一个属性被修饰后,那么当对象在序列化的过程中,这个属性会被忽略* 忽略不重要的属性可以达到减少内存的占用效果*/public Person(String name, int age, String gender, String[] otherInfo) {super();this.name = name;this.age = age;this.gender = gender;this.otherInfo = otherInfo;
}public String toString(){return name+","+age+","+gender+","+Arrays.toString(otherInfo);}}

5.1 实例写入操作(序列化)

ObjectOutputStream用于对象的序列化
ObjectOutputStream提供了writeObject方法
writeObject方法要求写出的对象所属的类必须实现接口:

  • Serializable _序列化接口 (签名接口,两个中的其一)
  • 否则会抛出异常

在流连接中,对象流的工作是:对象序列化,就是将给定的对象按照其结构转换为一组字节的过程。
然后这组字节再经过文件流写入文件,这个过程称为数据持久化,因为写入文件就是写入磁盘,断电也可保存;

package io;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;public class OOSDemo {public static void main(String[] args) throws IOException {/** 将一个Person实例写入文件person.obj*/String name = "苍老师";int age = 18;String gender="nv";String[]otherInfo = {"是个演员","来自日本"};Person p = new Person(name,age,gender,otherInfo);System.out.println(p);FileOutputStream fos = new FileOutputStream("person.dat");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(p);System.out.println("over");oos.close();}}

5.2 反序列化

ObjectInputStream用于对象的反序列化
ObjectInputStream提供的方法

  • Object readObject()
  • 该方法用于进行对象的反序列化,如果读取的字节不是一个 对象的时候(字节不是通过对象输出流序列化的),会抛异常;
package io;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;public class OISDemo {public static void main(String[] args) throws IOException, ClassNotFoundException {FileInputStream fis = new FileInputStream("person.dat");ObjectInputStream ois = new ObjectInputStream(fis);Person p = (Person) ois.readObject();System.out.println(p);ois.close();}}
练习
/*5 FileOutputStream fos1 = new FileOutputStream("./fos.txt");String a = "45456456";byte[]date = a.getBytes("utf-8");fos1.write(date);System.out.println("over");fos1.close();*//*4FileOutputStream fos1 = new FileOutputStream("./fos1.txt");String a = "wqeqwwe";byte[]date = a.getBytes();fos1.write(date);System.out.println("over");fos.close();*//*3FileOutputStream fos2 = new FileOutputStream("./fos2.txt");String s="asdas";byte[]date1 = s.getBytes();fos2.write(date1);System.out.println("over");fos2.close();*//*2FileOutputStream fos3 = new FileOutputStream("./fos3.txt");String as = "asdasdasd";byte[]date = as.getBytes();fos3.write(date);System.out.println("over");fos3.close();*//*1FileOutputStream fos4 = new FileOutputStream("./fos4.txt");String asd = "!@#!@#!@#";byte[]date1=asd.getBytes();fos4.write(date1);System.out.println("over");fos4.close();*/
/*5FileInputStream fis1 = new FileInputStream("./fos1.txt");byte[]date = new byte[200];int l = fis1.read(date);System.out.println("实际读取到了"+l+"个字节");String s = new String(date,0,l,"utf-8");System.out.println(s);fis1.close();*//*4FileInputStream fis2 = new FileInputStream("./fos2.txt");byte[]date = new byte[300];int l = fis2.read(date);System.out.println("实际读取到了"+l+"个字节");String sa = new String(date,0,l,"utf-8");System.out.println(sa);fis2.close();*//*3FileInputStream fis3 = new FileInputStream("./fos3.txt");byte[]date = new byte[400];int l=fis3.read(date);System.out.println("实际读取到了"+l+"个字节");String saa  = new String(date,0,l,"utf-8");System.out.println(saa);fis3.close();*//*2FileInputStream fis4 = new FileInputStream("./fos4.txt");byte[]date = new byte[200];int l = fis4.read(date);System.out.println("实际读取到了"+l+"个字节");String sa = new String(date,0,l,"utf-8");System.out.println(sa);fis4.close();/*/*1FileInputStream fis5 = new FileInputStream("./fos5.txt");byte[]date = new byte[200];int l = fis5.read(date);System.out.println("实际读取到了"+l+"个字节");String as = new String(date,0,l,"utf-8");System.out.println(as);fis5.close();*/
     /*5FileInputStream fis1 = new FileInputStream("image.png");BufferedInputStream bis1 = new BufferedInputStream(fis1);FileOutputStream fos1 = new FileOutputStream("image1.png");BufferedOutputStream bos1 = new BufferedOutputStream(fos1);int a=0;while((a=bis.read())!=-1){bos1.write(a);}System.out.println("over");bis1.close();bos1.close();*//*4 FileInputStream fis2 = new FileInputStream("image.png");FileOutputStream fos2 = new FileOutputStream("image2.png");BufferedInputStream bis2 = new BufferedInputStream(fis2);BufferedOutputStream bos2 = new BufferedOutputStream(fos2);int b = 0;while((b=bis2.read())!=-1){bos2.write(b);}System.out.println("over");bis2.close();bos2.close();*//*3FileInputStream fis3 = new FileInputStream("image.png");FileOutputStream fos3 = new FileOutputStream("image3.png");BufferedInputStream bis3 = new BufferedInputStream(fis3);BufferedOutputStream bos3 = new BufferedOutputStream(fos3);int s = 0;while((s = bis3.read())!=-1){bos3.write(s);}System.out.println("over");bis3.close();bos3.close();*//*2FileInputStream fis4 = new FileInputStream("ias.mp4");FileOutputStream fos4 = new FileOutputStream("ias1.mp4");BufferedInputStream bis4 = new BufferedInputStream(fis4);BufferedOutputStream bos4 = new BufferedOutputStream(fos4);int as = 0;while((as = bis4.read())!=-1){bos4.write(as);}System.out.println("over");bis4.close();bos4.close();*//*1FileInputStream fis0 = new FileInputStream("123.dat");FileOutputStream fos0 = new FileOutputStream("1234.dat");BufferedInputStream bis0 = new BufferedInputStream(fis0);BufferedOutputStream bos0 = new BufferedOutputStream(fos0);int dd = 0;while((dd = bis0.read())!=-1){bos0.write(dd);}System.out.println("over");bis0.close();bos0.close();*/
/*5FileOutputStream fos1 = new FileOutputStream("raf.txt");BufferedOutputStream bos1 = new BufferedOutputStream(fos1);String s = "sadasdsad";byte[]date = s.getBytes("utf-8");bos1.write(date);bos1.flush();System.out.println("over");bos1.close();*//*4FileOutputStream fos2 = new FileOutputStream("raf1.txt");BufferedOutputStream bos2 = new BufferedOutputStream(fos2);String sa = "!@#$@#$";byte[]data1 = sa.getBytes("utf-8");bos2.write(data1);bos2.flush();System.out.println("over");bos2.close();*//*3FileOutputStream fos3 = new FileOutputStream("raf2.txt");BufferedOutputStream bos3 = new BufferedOutputStream(fos3);String as = "dsasdasdas";byte[]date = as.getBytes("utf-8");bos3.write(date);bos3.flush();System.out.println("over");bos3.close();*//*2FileOutputStream fos4 = new FileOutputStream("raf3.txt");BufferedOutputStream bos4 = new BufferedOutputStream(fos4);String sa = "asddsa231123";byte[]data2 = sa.getBytes("utf-8");bos4.write(data2);bos4.flush();System.out.println("over");bos4.close();*//*1FileOutputStream fos0 = new FileOutputStream("raf0.txt");BufferedOutputStream bos5 = new BufferedOutputStream(fos0);String ds = "asasdasd";byte[]date1 = ds.getBytes("utf-8");bos5.write(date1);bos5.flush();System.out.println("over");bos5.close();*/
package io;import java.io.Serializable;
import java.util.Arrays;/*** 使用当前类测试对象流的对象读写操作* @author 毛**/
public class Person implements Serializable{/*** 序列号*/private static final long serialVersionUID = 1L;
private String name;
private int age;
private String gender;
private transient String[]otherInfo;
/** transient关键字* 当一个属性被修饰后,那么当对象在序列化的过程中,这个属性会被忽略* 忽略不重要的属性可以达到减少内存的占用效果*/public Person(String name, int age, String gender, String[] otherInfo) {super();this.name = name;this.age = age;this.gender = gender;this.otherInfo = otherInfo;
}public String toString(){return name+","+age+","+gender+","+Arrays.toString(otherInfo);}}
     /*5String name = "苍老师";int age = 18;String gender="nv";String[]otherInfo = {"是个演员","来自日本"};Person p = new Person(name,age,gender,otherInfo);FileOutputStream fos = new FileOutputStream("person.dat");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(p);System.out.println("over");oos.close();*//*4String name1 = "苍老师";int age1 = 18;String gender1="nv";String[]otherInfo1 = {"是个演员","来自日本"};Person p1 = new Person(name1,age1,gender1,otherInfo1);FileOutputStream fos1 = new FileOutputStream("person.dat");ObjectOutputStream oos1 = new ObjectOutputStream(fos1);oos1.writeObject(p1);System.out.println("over");oos1.close();*//*3String name2 = "苍老师";int age2 = 18;String gender2="nv";String[]otherInfo2 = {"是个演员","来自日本"};Person p2 = new Person(name2,age2,gender2,otherInfo2);FileOutputStream fos2 = new FileOutputStream("person.dat");ObjectOutputStream oos2 = new ObjectOutputStream(fos2);oos2.writeObject(p2);System.out.println("over");oos2.close();*//*2String name3 = "苍老师";int age3 = 18;String gender3="nv";String[]otherInfo3 = {"是个演员","来自日本"};Person p3 = new Person(name3,age3,gender3,otherInfo3);FileOutputStream fos3 = new FileOutputStream("person.dat");ObjectOutputStream oos3 = new ObjectOutputStream(fos3);oos3.writeObject(p3);System.out.println("over");oos3.close();*//*1String name4 = "苍老师";int age4 = 18;String gender4="nv";String[]otherInfo4 = {"是个演员","来自日本"};Person p4 = new Person(name4,age4,gender4,otherInfo4);FileOutputStream fos4 = new FileOutputStream("person.dat");ObjectOutputStream oos4 = new ObjectOutputStream(fos4);oos4.writeObject(p4);System.out.println("over");oos4.close();*/
/*5FileInputStream fis1 = new FileInputStream("person.dat");ObjectInputStream ois1 = new ObjectInputStream(fis1);Person p1 = (Person)ois1.readObject();System.out.println(p1);ois1.close();*//*4FileInputStream fis2 = new FileInputStream("person.dat");ObjectInputStream ois2 = new ObjectInputStream(fis2);Person p2 = (Person)ois2.readObject();System.out.println(p2);ois2.close();*//*3FileInputStream fis3 = new FileInputStream("person.dat");ObjectInputStream ois3 = new ObjectInputStream(fis3);Person p3 = (Person)ois3.readObject();System.out.println(p3);ois3.close();*//*2FileInputStream fis4 = new FileInputStream("person.dat");ObjectInputStream ois4 = new ObjectInputStream(fis4);Person p4 = (Person)ois4.readObject();System.out.println(p4);ois4.close();*//*1FileInputStream fis0 = new FileInputStream("person.dat");ObjectInputStream ois0 = new ObjectInputStream(fis0);Person p0 = (Person)ois0.readObject();System.out.println(p0);ois0.close();*/

JAVA SE学习day_05: IO与流操作相关推荐

  1. JAVA SE学习day_06:字符流、异常处理

    一.字符流 java将流按照读写单位划分为字节流与字符流 字节流:超类为InputStream和OutputStream,读写单位为字节 字符流:超类为Reader和Writer,读写单位为cahr ...

  2. JAVA SE学习day_03:包装类、文件操作——file

    一.包装类 包装类是为了解决基本类型不能直接参与面向对象开发的问题,实际就是将基本类型以"对象"的形式表示 Integer 1.1基本类型转换为对应的包装类(引用类型) int i ...

  3. 第一阶段Java SE学习概述

    Java SE学习概述 Java SE 学习分为五个阶段 第一部分:基础程序设计: 第二部分:面现象对象编程: 第三部分:Java SE 高级应用: 第四部分:JavaSE 新特性: 第五部分:MyS ...

  4. JAVA SE学习笔记(七):终章:Java程序设计基础笔记(全10万字)

    Java程序设计入门 ​                                              copyright © 2020 by 宇智波Akali 目录 文章目录 第1章 J ...

  5. Java SE 学习记录——进阶版11

    @学习记录 开始学习Java 遵从同学的指导,从Java se开始学习 黑马的JavaSE零基础入门[网络编程] 第一章 网络编程入门 1.1 软件结构 22-02 软件结构 1.2 网络通信协议 2 ...

  6. 安卓学习 Day23:文件流操作

    文件流操作 一.案例演示--文件操作 1.创建安卓应用 2.添加背景图片 3.字符串资源文件 4.主布局资源文件 5.在raw目录里创建文件 - test.txt 6.在assets目录里创建文件 - ...

  7. 【java基础】吐血总结Stream流操作

    文章目录 Stream流操作讲解 在这里插入图片描述 1 Stream概述 2 Stream与传统遍历对比 3 Stream的创建 4 Stream的使用 4.1 遍历/匹配(foreach.find ...

  8. JAVA SE 基础复习-IO与序列化(4)

    1.对象序列化 我们平时使用JAVA对象的时候,所有的操作都是在内存中进行的,即对象的生存周期不会比它所依赖的JVM更长.有时候我们又需要即使JVM已经停止,但是有能够在JVM停止后仍然能够获得之前的 ...

  9. Java SE 学习记录06

    @学习记录 开始学习Java 遵从同学的指导,从Java se开始学习 黑马的JavaSE零基础入门 day06-01 面向对象 package day06;import java.util.Arra ...

最新文章

  1. 孪生素数 java代码_科学网—孪生素数猜想——利用 Java + 正则表达式 输出孪生素数对 - 马廷灿的博文...
  2. 2018 我的学习分享路线
  3. 什么心态阻碍了你职业的发展
  4. 当 Android 开发者遇见 TensorFlow
  5. docker下载慢,卡顿解决办法——免费安装人人都有的docker加速器
  6. select下拉框option的样式修改
  7. HDFS : RemoteException Operation category READ is not supported in state standby.
  8. python3 namedtuple_去年发布的Python 3.8 稳定版,帮你们来一波特性全面解读
  9. MVC下c#对接微信公众平台开发者模式
  10. 软件项目建议书模板(免费)
  11. vue中使用粒子特效
  12. 国密(1) - 私钥Key文件( PEM格式)编解码方法
  13. jpress转换html5,JPress技术精讲:JPress如何做到安装后重新加载的?
  14. zimbra 证书过期--zimbra使用
  15. Python面向对象基础练习——设计一个名为 MyRectangle 的矩形类来表示矩形
  16. 诺基亚6300手机游戏下载_回忆杀!重温那些年你一定玩过的经典手机游戏,快看你玩过几个?...
  17. COLA的扩展性使用和源码研究
  18. Class 文件的魔数和文件版本号
  19. Python计算文件或字符串的MD5/SHA
  20. 2022年各大企业java面试题解析,堪称全网最详细的java面试指南

热门文章

  1. 最近公共祖先 LCA Tarjan算法
  2. Git 设置 SOCKS 代理
  3. .net数据根据字段进行分类(linq语句)
  4. mysql (master/slave)复制原理及配置
  5. 最短JS判断是否为IE6(IE的写法)
  6. Matlab(一) .jpg转.ppm
  7. 压力测试的几种常见性解决方案
  8. centos在yum install报错:Another app is currently holding the yum lock解决方法
  9. vegas pro 15解决导入的视频和音频有噪声问题,亲测可行
  10. Eclipse在高分屏下图标过小的解决方法