参考地址:
1. http://blog.csdn.net/yczz/article/details/38761237
File类
ObjectInputStream && ObjectOutputStream, 该流允许读取或写入用户自定义的类,但被读取或写入的类必须实现了Serializable接口
DataInputStream && DataOutputStream 有时没有必要存储整个对象的信息,而只是要存储一个对象的成员数据,成员数据的类型假设都是Java的基本数据类型,这样的需求不必使用到与Object输入、输出相关的流对象,可以使用DataInputStream、DataOutputStream来写入或读出数据
打算将Member类实例的成员数据写入文件中,并打算在读入文件数据后,将这些数据还原为Member对象。下面的代码简单示范了如何实现这个需求
1 package com.hxw.io; 2 3 class Student implements Serializable{ 4 private String name; 5 private int age; 6 7 public Student(String name, int age) { 8 super(); 9 this.name = name; 10 this.age = age; 11 } 12 13 @Override 14 public String toString() { 15 return "Student [name=" + name + ", age=" + age + "]"; 16 } 17 18 19 } 20 21 import java.io.*; 22 public class ObjetStream { 23 public static void main(String[] args) { 24 ObjectOutputStream objectwriter = null; 25 ObjectInputStream objectreader = null; 26 27 try { 28 objectwriter = new ObjectOutputStream(new FileOutputStream("D:/David/Java/java 高级进阶/files/student.txt")); 29 objectwriter.writeObject(new Student("gg", 22)); 30 objectwriter.writeObject(new Student("tt", 18)); 31 objectwriter.writeObject(new Student("rr", 17)); 32 33 objectreader=new ObjectInputStream(new FileInputStream("D:/David/Java/java 高级进阶/files/student.txt")); 34 for (int i = 0; i < 3; i++) { 35 System.out.println(objectreader.readObject()); 36 } 37 } catch (IOException | ClassNotFoundException e) { 38 // TODO自动生成的 catch 块 39 e.printStackTrace(); 40 }finally{ 41 try { 42 objectreader.close(); 43 objectwriter.close(); 44 } catch (IOException e) { 45 // TODO自动生成的 catch 块 46 e.printStackTrace(); 47 } 48 } 49 } 50 }
1 package com.hxw; 2 public class Member { 3 private String name; 4 private int age; 5 public Member() { 6 } 7 public Member(String name, int age) { 8 this.name = name; 9 this.age = age; 10 } 11 public void setName(String name){ 12 this.name = name; 13 } 14 public void setAge(int age) { 15 this.age = age; 16 } 17 public String getName() { 18 return name; 19 } 20 public int getAge() { 21 return age; 22 } 23 } 24 25 26 import java.io.*; 27 public class DataStreamDemo 28 { 29 public static void main(String[]args) 30 { 31 Member[] members = {new Member("Justin",90), 32 new Member("momor",95), 33 new Member("Bush",88)}; 34 try { 35 DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(args[0])); 36 37 for(Member member:members) 38 { 39 //写入UTF字符串 40 dataOutputStream.writeUTF(member.getName()); 41 //写入int数据 42 dataOutputStream.writeInt(member.getAge()); 43 } 44 45 //所有数据至目的地 46 dataOutputStream.flush(); 47 //关闭流 48 dataOutputStream.close(); 49 50 DataInputStream dataInputStream = new DataInputStream(new FileInputStream(args[0])); 51 52 //读出数据并还原为对象 53 for(int i=0;i<members.length;i++) 54 { 55 //读出UTF字符串 56 String name =dataInputStream.readUTF(); 57 //读出int数据 58 int score =dataInputStream.readInt(); 59 members[i] = new Member(name,score); 60 } 61 62 //关闭流 63 dataInputStream.close(); 64 65 //显示还原后的数据 66 for(Member member : members) 67 { 68 System.out.printf("%s %d%n",member.getName(),member.getAge()); 69 } 70 } 71 catch(IOException e) 72 { 73 e.printStackTrace(); 74 } 75 } 76 }