RandomAccessFile
随机访问文件,支持随机读写
- seek(long pos)绝对定位
- skipBytes(int num) 相对值
- read()
- write()
- RandomAccessFile(String str, String mode)
- r
- rw
- rws
- rwd
多线程复制文件
存储对象
流的关闭
1 import java.io.FileInputStream; 2 import java.io.FileOutputStream; 3 import java.io.ObjectInputStream; 4 import java.io.ObjectOutputStream; 5 6 import org.junit.Test; 7 8 public class ObjectSerializeDemo { 9 10 public static void main(String[] args) throws Exception { 11 //serialize(); 12 //deSerialize(); 13 serializePerson(); 14 deSerializePerson(); 15 } 16 17 /** 18 * 串行化方法 19 * @throws Exception 20 */ 21 private static void serialize() throws Exception { 22 // 一个对象 23 Integer i = new Integer(100); 24 FileOutputStream fos = new FileOutputStream("h:/a.data"); 25 26 27 // 通过文件输出流构造对象输出流 28 ObjectOutputStream oos = new ObjectOutputStream(fos); 29 oos.writeObject(i); 30 31 //关闭流 32 33 oos.close(); 34 //fos.close(); 35 System.out.println("over"); 36 } 37 38 /** 39 * 反串行化方法 40 * @throws Exception 41 */ 42 private static void deSerialize() throws Exception { 43 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("h:/a.data")); 44 Integer i = (Integer) ois.readObject(); 45 ois.close(); 46 47 System.out.println(i); 48 } 49 50 /** 51 * 串行化javabean方法 52 * @throws Exception 53 */ 54 @Test 55 private static void serializePerson() throws Exception { 56 Person p = new Person("tom", 21); 57 FileOutputStream fos = new FileOutputStream("h:/p.data"); 58 ObjectOutputStream oos = new ObjectOutputStream(fos); 59 oos.writeObject(p); 60 oos.close(); 61 //fos.close(); 62 System.out.println("over"); 63 } 64 65 /** 66 * 反串行化javabean方法 67 * @throws Exception 68 */ 69 private static void deSerializePerson() throws Exception { 70 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("h:/p.data")); 71 Person p = (Person) ois.readObject(); 72 ois.close(); 73 74 System.out.println(p.getName()); 75 } 76 }
1 import java.io.Serializable; 2 // 实现对象串行化接口 3 public class Person implements Serializable{ 4 private static final long serialVersionUID = -5854465328288308968L; 5 private String name; 6 private int age; 7 public Person() { 8 9 } 10 public Person(String name, int age) { 11 this.name = name; 12 this.age = age; 13 } 14 public String getName() { 15 return name; 16 } 17 public void setName(String name) { 18 this.name = name; 19 } 20 public int getAge() { 21 return age; 22 } 23 public void setAge(int age) { 24 this.age = age; 25 } 26 27 }
串行化(序列化)
将java对象转换成某种格式(JVM定义的)的字节数组
反串行化(序列化)
将字节数组恢复成java对象
java.io.Serializable
可串行接口 标志性接口
串行化应用场景
将对象用于网络间传输或本地化存储
深度复制
1 import java.io.FileInputStream; 2 import java.io.FileOutputStream; 3 import java.io.ObjectInputStream; 4 import java.io.ObjectOutputStream; 5 import java.io.Serializable; 6 7 import org.junit.Test; 8 9 /** 10 * 深度复制 11 * @author zhengguohuang 12 * 13 */ 14 public class DeepCopyDemo { 15 /** 16 * 串行化过程 17 * @throws Exception 18 */ 19 @Test 20 public void seria() throws Exception { 21 Person p = new Person(); 22 Son s = new Son(); 23 Dog d = new Dog(); 24 //设置关联关系 25 s.setPerson(p); 26 d.setPerson(p); 27 d.setSon(s); 28 //串行化 29 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("h://kkk.data")); 30 oos.writeObject(d); 31 oos.close(); 32 } 33 34 /** 35 * 反串行化过程 36 * @throws Exception 37 */ 38 @Test 39 public void deSeria() throws Exception { 40 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("h:/kkk.data")); 41 Dog d = (Dog) ois.readObject(); 42 ois.close(); 43 //System.out.println("son link:" + d.getSon().getPerson().getName());//NullPointException 44 System.out.println("dog link:" + d.getPerson().getName()); 45 } 46 } 47 48 class Person implements Serializable{ 49 50 /** 51 * 52 */ 53 private static final long serialVersionUID = -149817064278073579L; 54 private String name = "pName..."; 55 56 public String getName() { 57 return name; 58 } 59 60 public void setName(String name) { 61 this.name = name; 62 } 63 64 } 65 66 class Son implements Serializable{ 67 /** 68 * 69 */ 70 private static final long serialVersionUID = -2553176111008776359L; 71 // transient:临时的,穿行化链中断 72 private transient Person person; 73 private String name = "dogkkk"; 74 75 public Person getPerson() { 76 return person; 77 } 78 public void setPerson(Person person) { 79 this.person = person; 80 } 81 public String getName() { 82 return name; 83 } 84 public void setName(String name) { 85 this.name = name; 86 } 87 88 } 89 90 class Dog implements Serializable{ 91 /** 92 * 93 */ 94 private static final long serialVersionUID = -4168869841460181681L; 95 private Son son; 96 private String name; 97 private Person person; 98 public Son getSon() { 99 return son; 100 } 101 public void setSon(Son son) { 102 this.son = son; 103 } 104 public String getName() { 105 return name; 106 } 107 public void setName(String name) { 108 this.name = name; 109 } 110 public Person getPerson() { 111 return person; 112 } 113 public void setPerson(Person person) { 114 this.person = person; 115 } 116 117 }
反序列化对象时,是否经过构造函数,为什么
没有必要
成员变量中存储数据,成员函数只是行为,对象存在于堆区(对象的成员变量),串行化时抓取整个对象的状态,然后写到文件中去,反串行化时把属性值放在对应的位置,开辟空间放上对应值,不需要调用任何过程
ByteArrayInputStream&ByteArrayOutputStream
字节数组输入输出流,串行化到内存中
1 /** 2 * 使用ByteArrayInputStream + ByteArrayOutputStream实现对象图的复制 3 * @throws Exception 4 */ 5 public static void deeplyCopyInBAOS() throws Exception { 6 Person p = new Person(); 7 Son s = new Son(); 8 Dog d = new Dog(); 9 // 设置关联关系 10 s.setPerson(p); 11 d.setSon(s); 12 d.setPerson(p); 13 // 串行化 14 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 15 ObjectOutputStream oos = new ObjectOutputStream(baos); 16 oos.writeObject(d); 17 oos.close(); 18 baos.close(); 19 20 // 21 byte[] byteArray = baos.toByteArray(); 22 23 // 反串行化 24 ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); 25 ObjectInputStream ois = new ObjectInputStream(bais); 26 Dog d1 = (Dog) ois.readObject(); 27 ois.close(); 28 bais.close(); 29 System.out.println("dog link:" + d1.getPerson().getName()); 30 }
DataInputStream&DataOutputStream
1 import java.io.ByteArrayInputStream; 2 import java.io.ByteArrayOutputStream; 3 import java.io.DataInputStream; 4 import java.io.DataOutputStream; 5 6 import org.junit.Test; 7 8 public class DataIOStreamDemo { 9 10 /** 11 * DataArrayOutputStream 12 * @throws Exception 13 */ 14 @Test 15 public void write() throws Exception{ 16 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 17 DataOutputStream dos = new DataOutputStream(baos); 18 dos.writeByte(-1); //1 19 dos.writeShort(5); //2 20 dos.writeInt(10); //4 21 dos.writeLong(12L); //8 22 dos.writeFloat(0.5f); //4 23 dos.writeDouble(0.6); //8 24 dos.writeChar('a'); //2 25 dos.writeBoolean(true); //1 26 dos.close(); 27 baos.close(); 28 byte[] bytes = baos.toByteArray(); 29 System.out.println(bytes.length); 30 31 //反序列化 32 ByteArrayInputStream bais = new ByteArrayInputStream(bytes); 33 DataInputStream dis = new DataInputStream(bais); 34 System.out.println(dis.readByte()); 35 System.out.println(dis.readShort()); 36 dis.close(); 37 bais.close(); 38 } 39 }
- Reader
- writer
- BufferedReader
- BufferedWriter
- LineNumberReader
- InputStream
- FileInputStream
- ByteArrayInputStream
- ObjectInputStream
- DataInputStream
- FilterInputStream
- OutputStreamWriter