import java.io.*;
import java.util.Objects;
/**
* 过滤流
* */
public class Text{
public static void main(String[] args) throws IOException, ClassNotFoundException {
m5();
}
private static void m1() throws IOException {
DataOutputStream out=new DataOutputStream(new FileOutputStream("d:\\c.txt"));
out.writeInt(100);
out.writeBoolean(true);
out.writeUTF("哈哈哈159654");
out.close();
}
private static void m2() throws IOException {
DataInputStream in=new DataInputStream(new FileInputStream("d:\\c.txt"));
System.out.println(in.readInt()+50);
System.out.println(in.readBoolean());
System.out.println(in.readUTF());
in.close();
}
private static void m3() throws IOException {//数据缓存流
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("d:\\d.txt"));
out.write("啊啊啊".getBytes());
out.write("哈哈哈".getBytes());
out.write("嘿嘿嘿".getBytes());
out.close();
System.out.println("OK");
}
private static void m4() throws IOException {
Person p1=new Person("张三",22);
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("d:\\e.txt"));
out.writeObject(p1);
out.close();
}
private static void m5() throws IOException, ClassNotFoundException {
ObjectInputStream in=new ObjectInputStream(new FileInputStream("d:\\e.txt"));
Person p=(Person)in.readObject();
in.close();
System.out.println(p.getName()+"===="+p.getAge());
}
}
class Person implements Serializable{//用对象流去存储对象的前提是 将对象序列化 实现接口Serializable
private String name;
private int age;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && name.equals(person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, 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(){
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
}
过滤流(Java语言)
最新推荐文章于 2024-05-10 12:20:14 发布