各种各样的数据流—学完你懵不懵?-------------
基本数据操作流—Data。。
import java.io.*;public class Test {public static void main(String[] args) throws IOException {File file= new File("D:/Demo.txt");OutputStream ops= new FileOutputStream(file);DataOutputStream dos=new DataOutputStream(ops);dos.writeBoolean(false);dos.writeUTF("今天");dos.writeDouble(10.25);dos.writeByte(100);dos.close();}
}
这写入了些啥?
看不懂—乱码,因为是给机器看的,想要读取数据需要用到DataIntputStream
import java.io.*;public class Test {public static void main(String[] args) throws IOException {File file= new File("D:/Demo.txt");InputStream is= new FileInputStream(file);DataInputStream dis= new DataInputStream(is);System.out.println(dis.readBoolean());System.out.println(dis.readUTF());System.out.println(dis.readDouble());System.out.println(dis.readByte());
dis.close();}
}
public class DataOutputStream extends FilterOutputStream implements DataOutput {
public class DataInputStream extends FilterInputStream implements DataInput {
DataIntput 接口用于将数据从一系列字节转换为 Java 基本类型,并将这些字节读取为二进制流。
DataOutput 接口用于将数据从任意 Java 基本类型转换为一系列字节,并将这些字节写入二进制流。同时还提供了一个将 String 转换成 UTF-8 修改版格式并写入所得到的系列字节的工具。
对于此接口中写入字节的所有方法,如果由于某种原因无法写入某个字节,则抛出 IOException。
对象序列化----
import java.io.*;public class Test {public static void main(String[] args) throws IOException {File file= new File("D:/Hello.txt");OutputStream is= new FileOutputStream(file);ObjectOutputStream oos= new ObjectOutputStream(is);oos.writeObject(new String("我的老家,就住在这个屯"));oos.close();}
}
生成的文件看不懂,再写一个程序读取就可以了;
package HuiXin;import java.io.*;public class Test01 {public static void main(String[] args) throws IOException, ClassNotFoundException {File file= new File("D:/Hello.txt");InputStream is= new FileInputStream(file);ObjectInputStream ois= new ObjectInputStream(is);String per = (String) (ois.readObject());System.out.println(per);ois.close();}
}
自定义类序列化—要实现Serializable接口
import java.io.*;class Person implements Serializable {private static final long serialVersionUID = 1L;String name;int age;String gender;public Person(String name, int age, String gender) {this.name = name;this.age = age;this.gender = gender;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", gender='" + gender + '\'' +'}';}
}public class Test {public static void main(String[] args) throws IOException {File file= new File("D:/Hello.txt");OutputStream is= new FileOutputStream(file);ObjectOutputStream oos= new ObjectOutputStream(is);oos.writeObject(new Person("孙悟空",18,"男"));oos.close();}
}
接下来反序列化–
import java.io.*;public class Test01 {public static void main(String[] args) throws IOException, ClassNotFoundException {File file= new File("D:/Hello.txt");InputStream is= new FileInputStream(file);ObjectInputStream ois= new ObjectInputStream(is);Person per = (Person) (ois.readObject());System.out.println(per);ois.close();}
}
import java.io.*;class Person implements Serializable {private static final long serialVersionUID = 792646246687698667L;transient String name;static int age;String gender;public String getName() {return name;}public void setName(String name) {this.name = name;}public Person(String name, int age, String gender) {this.name = name;this.age = age;this.gender = gender;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", gender='" + gender + '\'' +'}';}
}public class Test {public static void main(String[] args) throws IOException {File file= new File("D:/Hello.txt");OutputStream is= new FileOutputStream(file);ObjectOutputStream oos= new ObjectOutputStream(is);oos.writeObject(new Person("孙悟空",18,"男"));oos.close();}
}
import java.io.*;public class Test01 {public static void main(String[] args) throws IOException, ClassNotFoundException {File file= new File("D:/Hello.txt");InputStream is= new FileInputStream(file);ObjectInputStream ois= new ObjectInputStream(is);Person per = (Person) (ois.readObject());System.out.println(per);ois.close();}
}
总结—
自定义数据序列化的时候必须要类中所有属性都要能够序列化;
静态属性—static的数据不能被序列化;
transenit修饰的也不能被序列化;