一、数据流
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();
}
}