IO流技术:带你玩转数据流、对象流、打印流

一、数据流

1、概述

  • 可以保留数据类型
  • DataOutputStream & DataInputStream
    1、先写出后读取
    2、读取的顺序和写出的顺序保持一致

还可以将ByteArrayOut/InputStream变成FileOut/InputStream

2、代码实现

public class DataTest {
	public static void main(String[] args) throws IOException {
		//写出
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos));
		dos.writeUTF("周百青");
		dos.writeInt(21);
		dos.writeChar('z');
		dos.writeBoolean(true);
		dos.flush();
		byte[] datas = baos.toByteArray();
		//读取
		DataInputStream dis = new DataInputStream(
				new BufferedInputStream(new ByteArrayInputStream(datas)));
		String name = dis.readUTF();
		int year = dis.readInt();
		char word = dis.readChar();
		boolean is = dis.readBoolean();
		System.out.println(name+year+word+is);
		System.out.println(datas.length);
	}
}

3、结果

在这里插入图片描述

二、对象流

1、概述

  • 序列化(Serialization):ObjectOutputStream
    反序列化(Deserialization):ObjectInputStream
  • 1、先写入后读取
    2、读取的顺序和写入的顺序一致
    3、只有实现 java.io.serialization 接口的对象才能被序列化

2、代码实现

基本数据类型会被保留下来,引用数据类型需要手动去转

public class ObjectTest {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//写出:序列化
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(baos));
		oos.writeUTF("周百青");
		oos.writeInt(21);
		oos.writeChar('z');
		oos.writeBoolean(true);
		//除了数据还支持对象
		oos.writeObject("代码是准确的描述");
		oos.writeObject(new Date());
		Employee emp = new Employee("周百青",666);
		oos.writeObject(emp);
		oos.flush();
		byte[] datas = baos.toByteArray();
		//读取:反序列化
		ObjectInputStream ois = new ObjectInputStream(
				new BufferedInputStream(new ByteArrayInputStream(datas)));
		String name = ois.readUTF();
		int year = ois.readInt();
		char word = ois.readChar();
		boolean is = ois.readBoolean();
		//对象的数据还原
		Object str = ois.readObject();
		Object date = ois.readObject();
		Object employee = ois.readObject();
		System.out.println(name+year+word+is);
		System.out.println(datas.length);
		
		//将类型转换还原,避免转换出错
		if(str instanceof String){
			String strObj = (String)str;
			System.out.println(strObj);
		}
		if(date instanceof Date){
			Date dateObj = (Date)date;
			System.out.println(dateObj);
		}
		if(employee instanceof Employee){
			Employee empObj = (Employee)employee;
			System.out.println(empObj.getName()+empObj.getSalary());
		}
	}
}

javabean 后期用于封装数据用

class Employee implements Serializable{
	private String name;
	//若不需要序列化则改为:
//	private transient String name;
	private double salary;
	public Employee(){
	}
	public Employee(String name,double salary){
		this.salary=salary;
		this.name=name;
	}
	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;
	}
}

3、结果

在这里插入图片描述

三、打印流

1、概述

  • PrintStream
    PrintWriter
  • 不需要手动 flush(),内部可自动刷新

2-1、代码实现

PrintStream的使用:

public class PrintTest {
	public static void main(String[] args) throws FileNotFoundException {
		//打印流 System.out
		PrintStream ps = System.out;
		ps.println("打印流");
		//加入 true 内部可自动刷新
		ps = new PrintStream(
				new BufferedOutputStream(
						new FileOutputStream("print.txt")),true);
		ps.println("666");
		ps.close();
		//重定向输出端(可打印输出到文件中)
		System.setOut(ps);
		System.out.println("print.txt");
		//重定向回控制台
		System.setOut(new PrintStream
				(new BufferedOutputStream
						(new FileOutputStream
								(FileDescriptor.out)),true)); //加入文件描述符
		System.out.println("print.txt");			
	}
}

3-1、结果

在这里插入图片描述
在这里插入图片描述

2-2、代码实现

PrintWriter的使用:

public class PrintTest2 {
	public static void main(String[] args) throws FileNotFoundException {
		PrintWriter pw = new PrintWriter(
				new BufferedOutputStream(
						new FileOutputStream("print.txt")),true);
		pw.println("周百青666");
		pw.close();
	}
}

3-2、结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值