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;
/*
* 数据流 DataInputStream DataOutputStream
* 1.先写出后读取
* 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(18);
dos.writeBoolean(false);
dos.writeChar('a');
dos.flush();
byte[] datas=baos.toByteArray();
//读
DataInputStream dis=new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
//顺序和写出保持一致
String msg=dis.readUTF();
int age=dis.readInt();
boolean flag=dis.readBoolean();
char ch=dis.readChar();
System.out.println(msg);
}
}
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 java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
/*
* 对象流 ObjectInputStream ObjectOutputStream
* 1.先写出后读取
* 2.读取的顺序与写出保持一致
* 3.不是所以对象都可以序列化,需要实现空接口Serializable
*/
public class OjectTest {
public static void main(String[] args)throws IOException, ClassNotFoundException {
//写-》序列化
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ObjectOutputStream dos=new ObjectOutputStream(new BufferedOutputStream(baos));
//操作数据类型+数据
dos.writeUTF("编码心酸泪");
dos.writeInt(18);
dos.writeBoolean(false);
dos.writeChar('a');
//加入对象
dos.writeObject("谁解其中味");
dos.writeObject(new Date());
Employee emp=new Employee("马云",400);
dos.writeObject(emp);
dos.flush();
byte[] datas=baos.toByteArray();
//读->反序列化
ObjectInputStream dis=new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
//顺序和写出保持一致
String msg=dis.readUTF();
int age=dis.readInt();
boolean flag=dis.readBoolean();
char ch=dis.readChar();
System.out.println(msg);
Object str=dis.readObject();
Object date=dis.readObject();
Object employee=dis.readObject();
if(str instanceof String) {//如果str是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());
}
}
}
class Employee implements java.io.Serializable{
private transient String name;//该数据不需要序列化
private double salary;
public Employee() {
super();
}
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
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;
}
}
对象流传文件版
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.util.Date;
/*
* 对象流 ObjectInputStream ObjectOutputStream
* 1.先写出后读取
* 2.读取的顺序与写出保持一致
* 3.不是所以对象都可以序列化,需要实现空接口Serializable
*/
public class OjectTest {
public static void main(String[] args)throws IOException, ClassNotFoundException {
//写-》序列化
ObjectOutputStream oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("c.txt")));
//操作数据类型+数据
oos.writeUTF("编码心酸泪");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('a');
//加入对象
oos.writeObject("谁解其中味");
oos.writeObject(new Date());
Employee emp=new Employee("马云",400);
oos.writeObject(emp);
oos.flush();
oos.close();
//读->反序列化
ObjectInputStream ois=new ObjectInputStream(new BufferedInputStream(new FileInputStream("c.txt")));
//顺序和写出保持一致
String msg=ois.readUTF();
int age=ois.readInt();
boolean flag=ois.readBoolean();
char ch=ois.readChar();
System.out.println(msg);
Object str=ois.readObject();
Object date=ois.readObject();
Object employee=ois.readObject();
if(str instanceof String) {//如果str是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());
}
ois.close();
}
}
class Employee implements java.io.Serializable{
private transient String name;//该数据不需要序列化
private double salary;
public Employee() {
super();
}
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
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;
}
}
import java.io.BufferedOutputStream;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
/*
* 打印流: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("打印流");
pw.println(true);
pw.close();
}
}