第十三篇:JAVA对象持久化

持久化就是将内存中的数据保存起来,使之可以长期存在。在Java中,我们可以把JAVA对象直接保存在文件中,在需要使用的时候,直接从文件中读取,这也是对象持久化的一种方式,在这一篇博客中,将演示两种将JAVA对象持久化到文件的方法,直接上代码:

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

/**
 * JAVA对象持久化
 * 
 * @author jianggujin
 * 
 */
public class JavaSerializable
{
   /**
    * 持久化为XML对象
    * 
    * @param obj
    * @param out
    */
   public void storeXML(Object obj, OutputStream out)
   {
      XMLEncoder encoder = new XMLEncoder(out);
      encoder.writeObject(obj);
      encoder.flush();
      encoder.close();
   }

   /**
    * 从XML中加载对象
    * 
    * @param in
    * @return
    */
   public Object loadXML(InputStream in)
   {
      XMLDecoder decoder = new XMLDecoder(in);
      Object obj = decoder.readObject();
      decoder.close();
      return obj;
   }

   /**
    * 持久化对象
    * 
    * @param obj
    * @param out
    * @throws IOException
    */
   public void store(Object obj, OutputStream out) throws IOException
   {
      ObjectOutputStream outputStream = new ObjectOutputStream(out);
      outputStream.writeObject(obj);
      outputStream.flush();
      outputStream.close();
   }

   /**
    * 加载对象
    * 
    * @param in
    * @return
    * @throws IOException
    * @throws ClassNotFoundException
    */
   public Object load(InputStream in) throws IOException,
         ClassNotFoundException
   {
      ObjectInputStream inputStream = new ObjectInputStream(in);
      Object obj = inputStream.readObject();
      inputStream.close();
      return obj;
   }

   public static void main(String[] args) throws Exception
   {
      String storeName = "java object";
      File xmlFile = new File("xmlFile.dat");
      JavaSerializable serializable = new JavaSerializable();
      serializable.storeXML(storeName, new FileOutputStream(xmlFile));
      System.out.println(serializable.loadXML(new FileInputStream(xmlFile)));
      File file = new File("file.dat");
      serializable.store(storeName, new FileOutputStream(file));
      System.out.println(serializable.load(new FileInputStream(file)));
   }
}
 
   
   
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

运行结果: 
Java object 
java object

我们可以打开示例中生成的两个文件: 
xmlFile.dat 
 
 
java object 

file.dat 
 t java object

我们可以发现xmlFile.dat实际上将JAVA对象持久化成了XML文件,file.dat以一种特殊的格式进行持久化,以文本模式打开后,部分内容为乱码。

你可能感兴趣的:(Java相关开发,java)