18 oos.writeObject(p1); 19 oos.writeObject(p2); 20 oos.close(); 21 } 22 }
IO流之对象操作流ObjectInputStream:
读取: new ObjectInputStream(InputStream), readObject() [Java] 纯文本查看 复制代码 ?
01 public class Demo3_ObjectInputStream { 02
03 /**
04 * @param args
05 * @throws IOException
06 * @throws ClassNotFoundException 07 * @throws FileNotFoundException 08 * 读取对象,反序列化 09 */
10 public static void main(String[] args) throws IOException, ClassNotFoundException { 11 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\12 Person p1 = (Person) ois.readObject();
黑马程序员济南中心 编著
13 Person p2 = (Person) ois.readObject(); 14 System.out.println(p1); 15 System.out.println(p2); 16 ois.close(); 17 } 18 19 }
IO流之对象操作流优化: 将对象存储在集合中写出 [Java] 纯文本查看 复制代码 ?
01 Person p1 = new Person(\张三\02 Person p2 = new Person(\李四\03 Person p3 = new Person(\马哥\04 Person p4 = new Person(\辉哥\05
06 ArrayList
黑马程序员济南中心 编著
11
12 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(\13 oos.writeObject(list); //写出集合对象 14
15 oos.close(); 读取到的是一个集合对象 [Java] 纯文本查看 复制代码 ?
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\1
ArrayList
相当于没有泛型 3
//想去掉黄色可以加注解 4
@SuppressWarnings(\5
for (Person person : list) { 6
System.out.println(person); 7
} 8
9
ois.close(); 数据输入输出流: 1.什么是数据输入输出流
* DataInputStream, DataOutputStream可以按照基本数据类型大小读写数据 * 例如按Long大小写出一个数字, 写出时该数据占8字节. 读取的时候也可以按照
黑马程序员济南中心 编著
Long类型读取, 一次读取8个字节. 2.使用方式
* DataOutputStream(OutputStream), writeInt(), writeLong() [Java] 纯文本查看 复制代码 ?
1 DataOutputStream dos = new DataOutputStream(new FileOutputStream(\2 dos.writeInt(997); 3 dos.writeInt(998); 4 dos.writeInt(999); 5
6 dos.close();
* DataInputStream(InputStream), readInt(), readLong() [Java] 纯文本查看 复制代码 ?
1 DataInputStream dis = new DataInputStream(new FileInputStream(\2 int x = dis.readInt(); 3 int y = dis.readInt(); 4 int z = dis.readInt(); 5 System.out.println(x); 6 System.out.println(y); 7 System.out.println(z);
黑马程序员济南中心 编著
8 dis.close(); 打印流的概述和特点: 1.什么是打印流
* 该流可以很方便的将对象的toString()结果输出, 并且自动加上换行, 而且可以使用自动刷出的模式
* System.out就是一个PrintStream, 其默认向控制台输出信息 [Java] 纯文本查看 复制代码 ?
1 PrintStream ps = System.out;
2 ps.println(97); //其实底层用的是Integer.toString(x),将x转换为数字字符串打印 3 ps.println(\
4 ps.println(new Person(\张三\5 Person p = null;
6 ps.println(p); //如果是null,就返回null,如果不是null,就调用对象的toString()
2.使用方式
* 打印: print(), println()
* 自动刷出: PrintWriter(OutputStream out, boolean autoFlush, String encoding) * 打印流只操作数据目的 [Java] 纯文本查看 复制代码 ?
1 PrintWriter pw = new PrintWriter(new FileOutputStream(\
黑马程序员济南中心 编著