当前位置:网站首页>2. Byte stream
2. Byte stream
2022-08-09 09:32:00 【come here my bear】
字节流
- 字节流的父类(抽象类):
- InputStream:字节输入流 (Superclass for all byte input stream classes)
- OutputStream:字节输出流 (Superclass for all byte output stream classes)
- 文件字节流 ( FileInputStream / FileOutputStream ) 不带缓存的 底层流
- 字节缓冲流 ( BufferedInputStream / BufferedOutputStream ) Enhance the efficiency of the underlying stream
- 提高IO效率,减少访问磁盘的次数
- Data is stored in the cache,flush是将缓存区的内容写入文件中,也可以直接close
- 对象流 ( ObjectInputStream(读取重构成对象) / ObjectOutputStream ) 增强缓冲区功能
- 增强缓冲区功能
- 增强了读写8basic types and string functions
- 增强了读写对象的功能:
- readObject() 从流中读取一个对象
- writeObject() 向流中写入一个对象
- 使用流传输对象的过程称为序列化、反序列化
- 注意事项:
- 序列化类必须实现Serializable接口
- 序列化类中对象属性要求实现Serializable接口
- 序列化版本号ID (serialVersionUID),保证序列化的类和反序列化的类是同一个类
- 使用transient(瞬间的) 修饰属性,这个属性不能序列化
- 静态属性不能被序列化
- 序列化多个对象,可以借助集合实现
文件字节流
- FileInputStream:
- int read(byte[] b) 从流中读取多个字节,将读到的内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1
- FileOutputStream:
- void write(byte[] b) 一次写多个字节,将b数组中的所有字节,写入输出流
- 如果写入时,There is no current file,Then the current file will be created under the path
- If it is not added, it is in append mode,Then every write is an overwrite
- FileOutputStream(String name, boolean append) 当(append)这个参数为true时,是追加模式,Each write appends to the end of the file
文件字节输入流
package com.io.zijie;
import java.io.FileInputStream;
/**
* FileInputStream 的使用
* 文件字节输入流
*/
public class Demo1 {
public static void main(String[] args) throws Exception{
// 创建FileInputStream 读取E:\\桌面\\aaa.txt文件
FileInputStream fis = new FileInputStream("E:\\桌面\\aaa.txt");
// 读取文件
// 单个字节读取
// System.out.println("------------单个字节读取--------");
// int data=0;
// while ((data=fis.read())!=-1){
// System.out.print((char)data);
// }
// 一次读取多个字节
System.out.println();
System.out.println("---------一次性读取多个字节------");
byte[] buf = new byte[3];
int count = 0; // 返回读取的字节数
while ((count=fis.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}
// 关闭流通道,释放资源
fis.close();
System.out.println("执行完毕");
}
}
文件字节输出流
package com.io.zijie;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* FileOutputStream的使用
* 文件字节输出流
*/
public class Demo2 {
public static void main(String[] args) throws IOException {
// 创建FileOutputStream对象 写入文件
FileOutputStream fos = new FileOutputStream("E:\\桌面\\aaa.txt",true);
// 写入文件
// fos.write(97); // 97在ASCIICorresponding in the table isa
// fos.write('b');
String str = "hello,world";
fos.write(str.getBytes());
// 关闭
fos.close();
System.out.println("执行完毕");
}
}
使用文件字节流实现文件的复制
package com.io.zijie;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Use file byte streams to achieve file copying
*/
public class Demo3 {
public static void main(String[] args) throws IOException {
// 创建流
// 创建文件输入流
FileInputStream fis = new FileInputStream("E:\\桌面\\aaa.txt");
// 创建文件输出流
FileOutputStream fos = new FileOutputStream("E:\\桌面\\bbb.txt");
// 一边读取一边写入
byte[] buf = new byte[1024];
int count = 0;
while ((count=fis.read(buf))!=-1){
fos.write(buf,0,count);
}
// 关闭
fis.close();
fos.close();
System.out.println("复制完毕");
}
}
字节缓冲流
- 缓冲流:BufferedInputStream/BufferedOutputStream
- 提高IO效率,减少访问磁盘的次数
- 数据存储在缓冲区中,flush是将缓冲区的内容写入文件中,也可以直接close(也会执行flush)
BufferedInputStream
package com.io.zijie;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
* 使用字节缓冲流
* BufferedInputStream
*/
public class Demo4 {
public static void main(String[] args) throws IOException {
// 创建BufferedInputStream对象 To enhance the underlying stream
FileInputStream fis = new FileInputStream("E:\\桌面\\aaa.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
// 读取
int data = 0;
while ((data=bis.read())!=-1){
System.out.print((char)data);
}
// 关闭
bis.close();
}
}
BufferedOutputStream
package com.io.zijie;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 使用字节缓冲流写入文件
* BufferedOutputStream
*/
public class Demo5 {
public static void main(String[] args) throws IOException {
// 创建BufferedOutputStream对象
FileOutputStream fos = new FileOutputStream("E:\\桌面\\aaa.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
// 写入文件
for (int i = 0; i < 10; i++) {
bos.write("加油".getBytes()); // getBytes() 返回字符数组
// bos.flush(); // 刷新到硬盘
}
// 关闭
bos.close();
System.out.println("执行完毕");
}
}
对象流
序列化类 Student类
package com.io.zijie;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 100L; // 序列化版本ID
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
ObjectInputStream 实现序列化(写入) Requires that serialization must be implementedSerializable接口
package com.io.zijie;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
/**
* 对象流
* ObjectOutputStream 序列化
*/
public class Demo6 {
public static void main(String[] args) throws IOException {
// 创建ObjectOutputStream对象
FileOutputStream fos = new FileOutputStream("E:\\桌面\\aaa.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
ArrayList<Student> list = new ArrayList<>();
Student s1 = new Student("张三", 11);
Student s2 = new Student("李四", 12);
list.add(s1);
list.add(s2);
// oos.writeObject(s1);
// oos.writeObject(s2);
oos.writeObject(list);
// 关闭
oos.close();
System.out.println("序列化完毕");
}
}
ObjectOutputStream 实现反序列化(读取)
package com.io.zijie;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
/**
* 对象流
* 实现反序列化(读取)
* ObjectInputStream
*/
public class Demo7 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 创建ObjectInputStream对象
FileInputStream fis = new FileInputStream("E:\\桌面\\aaa.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
// Student s = (Student)ois.readObject();
// Student s1 = (Student)ois.readObject();
Object s = ois.readObject();
// 关闭
ois.close();
System.out.println("反序列化完毕");
System.out.println(s);
// System.out.println(s1);
}
}
边栏推荐
- HD Satellite Map Browser
- Ontology development diary 02 - simple sparql query
- What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
- 8.递归遍历和删除案例
- Do you know the basic process and use case design method of interface testing?
- unittest测试框架原理及测试流程解析,看完绝对有提升
- Django实现对数据库数据增删改查(一)
- Lecture 4 SVN
- 接口测试的基础流程和用例设计方法你知道吗?
- 一个项目的整体测试流程有哪几个阶段?测试方法有哪些?
猜你喜欢
随机推荐
How much do you know about the mobile APP testing process specifications and methods?
【个人学习总结】CRC校验原理及实现
.ts 音频文件转换成 .mp3 文件
Django实现对数据库数据增删改查(一)
Web请求原理
MySQL Checking and Filling Leaks (5) Unfamiliar Knowledge Points
Jfinal loading configuration file principle
Django实现对数据库数据增删改查(二)
常用功能测试的检查点与用例设计思路
5.转换流
Anti App so层对抗分析
使用Protege4和CO-ODE工具构建OWL本体的实用指南-1.3版本(4.Building An OWL Ontology)
MySQL lock
Redis Basics
可以写进简历的软件测试项目实战经验(包含电商、银行、app等)
TestNG使用教程详解
Onnx - environment build 】 【 tensorrt
接口测试主要测试哪方面?需要哪些技能?要怎么学习?
JS-常用方法整理
【面试体系知识点总结】---JVM