zoukankan      html  css  js  c++  java
  • Java基础之序列化对象——将对象写入到文件中(SerializeObjects)

    控制台程序。

    首先定义一个含有任意不同数据类型域的可序列化类:

     1 import java.io.Serializable;
     2 
     3 public class Junk implements Serializable {
     4   private static java.util.Random generator = new java.util.Random();
     5   private int answer;                                                  // The answer
     6   private double[] numbers;                                            // Valuable data
     7   private String thought;                                              // A unique thought
     8   private static final long serialVersionUID = 9001L;
     9 
    10   public Junk(String thought) {
    11     this.thought = thought;
    12     answer = 42;                                                       // Answer always 42
    13 
    14     numbers = new double[3 + generator.nextInt(4)];                    // Array size 3 to 6
    15     for(int i = 0 ; i < numbers.length ; ++i) {                        // Populate with
    16       numbers[i] = generator.nextDouble();                             // random values
    17    }
    18   }
    19 
    20   @Override
    21   public String toString() {
    22     StringBuffer strBuf = new StringBuffer(thought);
    23     strBuf.append('
    ').append(String.valueOf(answer));
    24     for(int i = 0 ; i < numbers.length ; i++) {
    25       strBuf.append("
    numbers[")
    26             .append(String.valueOf(i))
    27             .append("] = ")
    28             .append(numbers[i]);
    29     }
    30     return strBuf.toString();
    31   }
    32 }

    然后使用如下程序能将这个类类型的对象写入到文件中:

     1 import java.io.*;
     2 import java.nio.file.*;
     3 import java.io.IOException;
     4 
     5 public class SerializeObjects {
     6   public static void main(String[] args) {
     7     Junk obj1 = new Junk("A green twig is easily bent.");
     8     Junk obj2 = new Junk("A little knowledge is a dangerous thing.");
     9     Junk obj3 = new Junk("Flies light on lean horses.");
    10 
    11     Path file = Paths.get(System.getProperty("user.home")).resolve("Beginning Java Struff").resolve("JunkObjects.bin");
    12     try {
    13       Files.createDirectories(file.getParent());    // Make sure we have the directory
    14     } catch (IOException e) {
    15       e.printStackTrace();
    16       System.exit(1);
    17     }
    18 
    19     try (ObjectOutputStream objectOut = new ObjectOutputStream(new BufferedOutputStream(Files.newOutputStream(file)))){
    20       // Write three objects to the file
    21       objectOut.writeObject(obj1);                                     // Write object
    22       objectOut.writeObject(obj2);                                     // Write object
    23       objectOut.writeObject(obj3);                                     // Write object
    24       System.out.println("
    
    obj1:
    " + obj1
    25                         +"
    
    obj2:
    " + obj2
    26                         +"
    
    obj3:
    " + obj3);
    27 
    28     } catch(IOException e) {
    29       e.printStackTrace();
    30     }
    31   }
    32 }
  • 相关阅读:
    从零开始写STL—哈希表
    从零开始写STL-string类型
    从零开始写STL—模板元编程之any
    从零开始写STL—模板元编程之tuple
    c++ 实现 key-value缓存数据结构
    从零开始写STL
    从零开始写STL—functional
    从零开始写STL—set/map
    从零开始写STL-二叉搜索树
    洛谷 P4016 负载平衡问题
  • 原文地址:https://www.cnblogs.com/mannixiang/p/3409389.html
Copyright © 2011-2022 走看看