关于JavaIO流学习总结(2)
JAVA基础入门
十二、Print流
Print流只有输出流无输入流,PrintWriter和PrintStream分别针对字符字节
两个类提供了重载的Print和Println方法用于多种数据类型的输出
PrintWriter和PrintStream的输出操作不会抛出异常
PrintWriter和PrintStream有自动flush功能
----System.setOut(接收OutputStream类):用于设置系统默认输出流
十三、Object流
等同于c#序列化,用直接将Object写入或读出
transient关键字为不序列化此成员变量
需要序列化的类必须实现Serializable接口
主要方法:writeObject(Object); readObject();
读出为Object类型需要强转数据类型
1 import java.io.*;
2
3 public class TestObjectIO {
4 public static void main(String args[]) throws Exception {
5 T t = new T();
6 t.k = 8;
7 FileOutputStream fos = new FileOutputStream("d:/share/java/io/testobjectio.dat");
8 ObjectOutputStream oos = new ObjectOutputStream(fos);
9 oos.writeObject(t);
10 oos.flush();
11 oos.close();
12
13 FileInputStream fis = new FileInputStream("d:/share/java/io/testobjectio.dat");
14 ObjectInputStream ois = new ObjectInputStream(fis);
15 T tReaded = (T)ois.readObject();
16 System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);
17
18 }
19 }
20
21 class T
22 implements Serializable
23 {
24 int i = 10;
25 int j = 9;
26 double d = 2.3;
27 transient int k = 15;
28 }
3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/