【问题标题】:How do I deserialize a file?如何反序列化文件?
【发布时间】:2018-04-28 16:07:16
【问题描述】:

我需要 #3 和 #5 方面的帮助。

  1. 创建一个集合并用 1 到 15(含)之间的 20 个随机数填充它

    List<Integer> list1 = new ArrayList<Integer>(Arrays.asList(
            rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1),
            rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1),
            rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1),
            rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1), rand.nextInt(15 + 1)
       ));
    
  2. 打印数字

    System.out.println(list1);
    
  3. 为 Random 类的构造函数提供值为 13 的种子。

    • 播种 ctor 是什么意思?
  4. 将集合序列化为名为 Numbers.ser 的文件

    try (ObjectOutputStream serialize = new ObjectOutputStream(new FileOutputStream("src/serialize/Numbers.ser"))) {
        serialize.writeObject(list1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    
  5. 将文件反序列化为一个名为 numberFromFile 的新集合。删除所有重复的数字

    • 这是我对如何反序列化感到困惑的地方。解释是什么?

【问题讨论】:

标签: java serialization deserialization


【解决方案1】:
//Seed the constructor of class Random with the value 13.
Random rand = new Random(13);

//Create a collection and fill it with 20 random numbers between 1 and 15 (inclusive)

List<Integer> list1 = new ArrayList<>();

for (int i = 0; i < 20; i++) {
    list1.add(rand.nextInt(15) + 1);  // Note here
}

//Print the numbers

System.out.println(list1);

//Serialize the collection to a file called Numbers.ser

try (ObjectOutputStream out = new ObjectOutputStream(
        new FileOutputStream("Numbers.ser"))) {
    out.writeObject(list1);
} catch (IOException e) {
    e.printStackTrace();
}

//Deserialize the file into a new collection called numberFromFile. Remove all duplicate numbers
//this is where I am confused on how to deserialize. Can you someone explain this to me?
List<Integer> numberFromFile = null;
try (ObjectInputStream in = new ObjectInputStream(
        new FileInputStream("Numbers.ser"))) {
    numberFromFile = (List<Integer>) in.readObject();
} catch (Exception e) {
    e.printStackTrace();
}

//System.out.println(numberFromFile);

Set<Integer> distinct = new HashSet<>(numberFromFile); 
System.out.println(distinct);

【讨论】:

  • 我尝试了您的代码,在反序列化时,它会打印相同的列表两次,其中包含重复项。如何打印不重复?
  • 试试这个.. Set set = new HashSet(list2); System.out.println(set);
【解决方案2】:

使用种子初始化Random 意味着它将再次生成相同的随机数序列。只需查看文档:https://docs.oracle.com/javase/8/docs/api/java/util/Random.html

要从文件中读取不同的数字,您可以执行以下操作:

try(ObjectInputStream deserialize = new ObjectInputStream(new FileInputStream("src/serialize/Numbers.ser"))) {
    List<Integer> list2 = (List<Integer>) deserialize.readObject();
    System.out.println(list2.stream().distinct().collect(Collectors.toList()));
} catch(...) { ... }

【讨论】:

    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2021-12-21
    相关资源
    最近更新 更多