JAVA基础——IO流(三)——序列流及序列化,内存输出流

一、 序列流

  1. 什么是序列流
    序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.
  2. 使用方式
    整合两个: SequenceInputStream(InputStream, InputStream)

案例:未使用序列流前

FileInputStream fileInputStream1 =new FileInputStream("a.txt");		
//创建字节输入流关联a.txt
FileOutputStream fileOutputStream =new FileOutputStream("c.txt");	
//创建字节输出流关联c.txt
		int b1;
		while ((b1=fileInputStream1.read())!=-1) {
    
    							
			fileOutputStream.write(b1);
}
fileInputStream1.close();
		int b2;
FileInputStream fileInputStream2 =new FileInputStream("b.txt");
		while ((b2=fileInputStream2.read())!=-1) {
    
    
			fileOutputStream.write(b2);
}
fileInputStream2.close();
fileOutputStream.close();

使用SquenceInputStream,可以将两个流整合成一个流

FileInputStream fis1 =new FileInputStream("a.txt");
FileInputStream fis2 =new FileInputStream("b.txt");
SequenceInputStream sis =new SequenceInputStream(fis1,fis2);
FileOutputStream fos =new FileOutputStream("c.txt");
		int b;
while ((b=sis.read())!=-1) {
    
    
			fos.write(b);
	}
sis.close();
fos.close();

效果如下:
在这里插入图片描述

二、 序列流整合多个

应用场景,我们可以做一个歌曲串烧。将自己喜欢听的歌整合成一首歌曲。

FileInputStream fis1=new FileInputStream("a.txt");
FileInputStream fis3=new FileInputStream("c.txt");
Vector<FileInputStream> vector =new Vector<FileInputStream>();
		vector.add(fis1);
		vector.add(fis2);
		vector.add(fis3);
		
Enumeration<FileInputStream> en =vector.elements();
SequenceInputStream sis =new SequenceInputStream(en);
FileOutputStream fos  =new FileOutputStream("d.txt");
		int b;
		while ((b=sis.read())!=-1) {
    
    
			fos.write(b);
		}
sis.close();
fos.close();

效果如下:
在这里插入图片描述

三、 内存输出流

  1. 什么是内存输出流
    该输出流可以向内存中写数据, 把内存当作一个缓冲区, 写出之后可以一次性获取出所有数据
  2. 使用方式
    创建对象: new ByteArrayOutputStream()
    写出数据: write(int), write(byte[])
    获取数据: toByteArray()
FileInputStream fileInputStream =new FileInputStream("e.txt");
	//在内存中创建了可以增长的内存数组
ByteArrayOutputStream baos =new ByteArrayOutputStream();
		 
		int b;
		while ((b=fileInputStream.read())!=-1) {
    
    
		baos.write(b);
	}
//将缓冲区的庶几乎全部获取出来,并赋值给arr数组
	   byte []arr=baos.toByteArray();
		System.out.println(new String(arr));
//上面两句可以直接转换直接用下面的语句进行赋值
		System.out.println(baos.toString());	
//将缓冲区的内容转换为字符串,可以省略调用toString方法
		fileInputStream.close();

注意byte []arr=baos.toByteArray();
System.out.println(new String(arr));

可以替换为== System.out.println(baos.toString());==
该功能可以调用聊天记录,使用数据库文件调用本地的数据,并且存储到内容中可以一次性调用出来。

四、 对象操作流ObjecOutputStream——序列化

  1. .什么是对象操作流
    该流可以将一个对象写出, 或者读取一个对象到程序中. 也就是执行了序列化和反序列化的操作。类似于我们玩游戏,将游戏人物的等级,装备以对象的形式存储在数据库或者服务器中
  2. 使用方式
    写出: new ObjectOutputStream(OutputStream), writeObject()
    案例:
    第一步:
    新建一个domain类,注意如果想要实现序列化就必须引用Serializable接口
public class Person implements Serializable {
    
    
	private String name;
	private int 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;
	}
	public Person() {
    
    
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
    
    
		return "Person [name=" + name + ", age=" + age + "]";
	}
	public Person(String name, int age) {
    
    
		super();
		this.name = name;
		this.age = age;
	}
	
}

第二步:读取数据

Person p1=new Person("张三",12);
Person p2=new Person("李四",13);
ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("e.txt"));
		oos.writeObject(p1);
		oos.writeObject(p2);
		oos.close();

效果如下:
在这里插入图片描述

五、 对象操作流ObjectInputStream——反序列化

读取: new ObjectInputStream(InputStream), readObject()

public static void main(String[] args) throws IOException, ClassNotFoundException{
    
    
		ObjectInputStream ois =new ObjectInputStream(new FileInputStream("e.txt"));
		Person p1=(Person)ois.readObject();
		Person p2=(Person)ois.readObject();
		
		System.out.println(p1);
		System.out.println(p2);
		ois.close();
		
	}

效果如下:
在这里插入图片描述
如果多写一个对象,会进行报错。

六、 对象操作流的优化

将对象存储在集合中写出,然后将集合只进行一次书写,最后读取的时候只读取一个集合即可,再对集合进行遍历
先进行写的操作

Person p1=new Person("张三",12);
		Person p2=new Person("李四",13);
		Person p3=new Person("王五",14);
		Person p4=new Person("赵六",15);
		List<Person> list =new ArrayList<Person>();
		list.add(p1);
		list.add(p1);
		list.add(p3);
		list.add(p4);
		ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("e.txt"));
		oos.writeObject(list);
		oos.close();

进行读的操作

ObjectInputStream ois =new ObjectInputStream(new FileInputStream("e.txt"));
ArrayList<Person>list=(ArrayList<Person>)ois.readObject();
		for (Person person : list) {
    
    
			System.out.println(person);
}
ois.close();

将上面两个操作进行和写。

猜你喜欢

转载自blog.csdn.net/Mr_GYF/article/details/108818136