IO_数据流和对象流

目录

数据流

数据流实验代码

对象流

对象流实验代码


数据流

   数据流:用于操作基本数据类型和字符串 数据流将“基本数据类型与字符串类型”作为数据源,
 从而允许程序以与机器无关的方式 从底层输入输出流中操作 Java 基本数据类型与字符串类型
   1.先写入后读取
   2.读取顺序与写入保持一致


数据流实验代码

package cn.sxt.IO02;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import cn.sxt.IO.ByteArrayCopy;

/*
 * 数据流:用于操作基本数据类型和字符串
 * 
 * 1.先写入后读取
 * 2.读取顺序与写入保持一致
 * 数据流将“基本数据类型与字符串类型”作为数据源,
 * 从而允许程序以与机器无关的方式 从底层输入输出流中操作 
 * Java 基本数据类型与字符串类型
 * DataOutputStream dos =new DataOutputStream(字节输出流) 文件输出流 或者字节数组
 */
public class Date_io {
	
  public static void main(String[] args) throws IOException {
	 //写入
	ByteArrayOutputStream bos=new ByteArrayOutputStream();//有一个特殊方法 toByteArray();可以将任何数据类型转换成字节数组
	DataOutputStream dos =new DataOutputStream(new BufferedOutputStream(bos));//加上缓冲流
	//操作数据类型+数据
	dos.writeUTF("fashk");
	dos.writeInt(19);
	dos.writeBoolean(true);
	dos.writeChar('s');
	dos.flush();
	byte date[]=bos.toByteArray();
	//读取
	DataInputStream dis=new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(date)) );
	//顺序跟写出一致
	String msg= dis.readUTF();
	System.out.println(msg);
	
}
}

对象流

对象流:比数据流多了操作对象的功能

   1.先写入后读取
   2.读取顺序与写入保持一致

  3.对象数据还原

对象流实验代码

package cn.sxt.IO02;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Object_IO {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
	  
		ObjectOutputStream oos =new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("test")));//加上缓冲流
		
		oos.writeUTF("fashk");
		//oos.writeInt(19);
		//oos.writeBoolean(true);
		//oos.writeChar('s');
		//对象
		oos.writeObject("sg");
		Employee emp =new Employee("zt",50);
		oos.writeObject(emp);
		oos.flush();
		oos.close();
		//读取
		ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("test")) );
		//顺序跟写出一致
		String msg= ois.readUTF();
		System.out.println(msg);
		//对象数据还原
		Object str =ois.readObject();
		Object empl=ois.readObject();
		if (str instanceof String) {
			String str1 = (String) str;
			System.out.println(str1);
		}
		if (empl instanceof Employee) {
			Employee empl1 = (Employee) empl;
			System.out.println(empl1.getName()+empl1.getSalary());
		}
}
}

 class Employee implements Serializable{
	 private transient String name;
	 private double salary;
	public Employee(String name, double salary) {
		super();
		this.name = name;
		this.salary = salary;
	}
	public Employee() {
		super();
		
	}
	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;
	}
	 
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值