当前位置:网站首页>2.字节流
2.字节流
2022-08-09 09:23:00 【过来我的小熊】
字节流
- 字节流的父类(抽象类):
- InputStream:字节输入流 (所有字节输入流类的超类)
- OutputStream:字节输出流 (所有字节输出流类的超类)
- 文件字节流 ( FileInputStream / FileOutputStream ) 不带缓存的 底层流
- 字节缓冲流 ( BufferedInputStream / BufferedOutputStream ) 增强底层流的效率
- 提高IO效率,减少访问磁盘的次数
- 数据存储在缓存区中,flush是将缓存区的内容写入文件中,也可以直接close
- 对象流 ( ObjectInputStream(读取重构成对象) / ObjectOutputStream ) 增强缓冲区功能
- 增强缓冲区功能
- 增强了读写8种基本类型和字符串功能
- 增强了读写对象的功能:
- readObject() 从流中读取一个对象
- writeObject() 向流中写入一个对象
- 使用流传输对象的过程称为序列化、反序列化
- 注意事项:
- 序列化类必须实现Serializable接口
- 序列化类中对象属性要求实现Serializable接口
- 序列化版本号ID (serialVersionUID),保证序列化的类和反序列化的类是同一个类
- 使用transient(瞬间的) 修饰属性,这个属性不能序列化
- 静态属性不能被序列化
- 序列化多个对象,可以借助集合实现
文件字节流
- FileInputStream:
- int read(byte[] b) 从流中读取多个字节,将读到的内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1
- FileOutputStream:
- void write(byte[] b) 一次写多个字节,将b数组中的所有字节,写入输出流
- 如果写入时,没有当前文件,那么就会在路径下创建当前文件
- 如果没有加是追加模式,那么每次写入都是覆盖
- FileOutputStream(String name, boolean append) 当(append)这个参数为true时,是追加模式,每次写入都会追加到文件末尾
文件字节输入流
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在ASCII表中对应是a
// 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;
/**
* 使用文件字节流来实现文件的复制
*/
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对象 为了增强底层流
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 实现序列化(写入) 要求序列化必须实现Serializable接口
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);
}
}
边栏推荐
- 性能测试的基本概念是什么?做好性能测试需要掌握哪些知识?
- on duplicate key update
- 如何用数组实现环形队列
- Domestic Google earth, terrain analysis seconds kill with the map software
- 高清卫星地图浏览器
- What are the basic concepts of performance testing?What knowledge do you need to master to perform performance testing?
- 使用图新地球无法加载谷歌地球的完美解决方法(附软件下载)
- Swap swap partition
- seata处理分布式事务
- 运行flutter项目时遇到的问题
猜你喜欢

MVCC multi-version concurrency control
选择黑盒测试用例设计方法的综合策略方案总结

Understanding of PID control motor output as motor PWM duty cycle input

The div simulates the textarea text box, the height of the input text is adaptive, and the word count and limit are implemented

MySQL锁

白盒测试的概念、目的是什么?及主要方法有哪些?

如何用数组实现环形队列

Redis high availability

上帝视角看高清村庄卫星地图,附下载高清卫星地图最新方法

游戏测试的概念是什么?测试方法和流程有哪些?
随机推荐
这12个GIS软件一个比一个好用
问卷问题和答案的合并
“摄像头用不了”+win8.1+DELL+外置摄像头+USB免驱的解决办法
Django实现对数据库数据增删改查(二)
MySQL transaction isolation
MySQL Leak Check (4) Stored Procedures and Cursors
TypeScript简记(一)
绝了,这套RESTful API接口设计总结
单元测试是什么?怎么写?主要测试什么?
Summary of steps and methods for installing and uninstalling test cases that you must read
软件测试面试中,面试官问你一些比较“刁难”的问题你会怎么回答
on duplicate key update
MySQL索引
Redis high availability
Jfinal loading configuration file principle
条件和递归
自动化测试简历编写应该注意哪方面?有哪些技巧?
本体开发日记05-努力理解SWRL(上)
lateral view explode的另一种实现方式
栈的实现之用链表实现