DataOutputStream(数据输出流)
- 允许把数据和其类型一并写出去
构造器 | 说明 |
---|---|
public DataOutputStream(OutputStream out) | 创建新数据输出流包装基础的字节输出流 |
方法 | 说明 |
---|---|
public final void writeByte(int v) throws IOException | 将byte类型的数据写入基础的字节输出流 |
public final void writeInt(int v) throws IOException | 将int类型的数据写入基础的字节输出流 |
public final void writeDouble(Double v) throws IOException | 将double类型的数据写入基础的字节输出流 |
public final void writeUTF(String str) throws IOException | 将字符串数据以UTF-8编码成字节写入基础的字节输出流 |
public void write(int/byte[]/byte[]一部分) | 支持写字节数据出去 |
DataInputStream(数据输入流)
- 用于读取数据输出流写出去的数据
构造器 | 说明 |
---|---|
public DataInputStream(InputStream is) | 创建新数据输入流包装基础的字节输入流 |
方法 | 说明 |
---|---|
Public final byte readByte() throws IOException | 读取字节数据返回 |
public final int readInt() throws IOException | 读取int类型的数据返回 |
public final double readDouble() throws IOException | 读取double类型的数据返回 |
public final String readUTF() throws IOException | 读取字符串数据(UTF-8)返回 |
public int readInt()/read(byte[]) | 支持读字节数据进来 |
演示1:DataOutputStream(数据输出流)
public class DataOutputStreamTest {
public static void main(String[] args) {
try (
// 创建一个数据输出流包装低级的字节输出流
DataOutputStream dos = new DataOutputStream(new FileOutputStream("oop3\\src\\com\\ligemanyin\\_IO\\DataStream\\DataOut.txt"));
) {
dos.writeInt(100);
dos.writeDouble(100.0);
dos.writeBoolean(true);
dos.writeByte((byte) 100);
dos.writeChar('a');
dos.writeShort((short) 100);
dos.writeLong(1000000000000000000L);
dos.writeFloat(100.0f);
dos.writeUTF("hello world");
// DataOutputStream写出的数据不支持预览,仅支持DataInputStream读入
} catch (Exception e) {
e.printStackTrace();
}
}
}
演示2:DataInputStream
public class DataInputStreamTest {
public static void main(String[] args) {
try (
DataInputStream dis = new DataInputStream(new FileInputStream("oop3\\src\\com\\ligemanyin\\_IO\\DataStream\\DataOut.txt"));
) {
// 按照写入数据类型逐个读取
int i = dis.readInt();
System.out.println(i);
double d = dis.readDouble();
System.out.println(d);
boolean bol = dis.readBoolean();
System.out.println(bol);
byte b = dis.readByte();
System.out.println(b);
char c = dis.readChar();
System.out.println(c);
short s = dis.readShort();
System.out.println(s);
long l = dis.readLong();
System.out.println(l);
float f = dis.readFloat();
System.out.println(f);
String str = dis.readUTF();
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}
序列化流
ObjectOutputStream(对象字节输出流)
- 可以把Java对象进行序列化:把Java对象存入到文件中去
构造器 | 说明 |
---|---|
public ObjectOutputStream(OutputStream out) | 创建对象字节输出流,包装基础的字节输出流 |
方法|说明
public final void writeObject(Object o) throws IOException|把对象写出去
注:对象如果要参与序列化,必须实现序列化接口(java.io.Serializable)
ObjectInputStream(对象字节输入流)
- 可以把Java对象进行反序列化:把存储在文件中的Java对象读入到内存中来
构造器 | 说明 |
---|---|
public ObjectInputStream(InputStream is) | 创建对象字节输入流 |
方法 | 说明 |
---|---|
public final Object readObject() | 把存储在文件中的Java对象读出来 |
注:如果想让某个成员变量不参与反序列化(如密码字段),可在该变量前声明 transient
演示1:对象序列化
实例类自行设计
public class ObjectOutputStreamTest {
public static void main(String[] args) {
try (
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("oop3\\src\\com\\ligemanyin\\_IO\\ObjectStream\\ObjectOut.txt"));
) {
User u = new User("李华", 18, "男", "上海");
oos.writeObject(u);
} catch (Exception e) {
e.printStackTrace();
}
}
}
演示2:反序列化
public class ObjectInputStreamTest {
public static void main(String[] args) {
try (
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("oop3\\src\\com\\ligemanyin\\_IO\\ObjectStream\\ObjectOut.txt"));
) {
User user = (User) ois.readObject();
System.out.println(user.getName());
System.out.println(user.getAge());
System.out.println(user.getSex());
System.out.println(user.getAddress());
} catch (Exception e) {
e.printStackTrace();
}
}
}