【问题标题】:Java deserialize Arrays.toString() - human readable serialization - config fileJava 反序列化 Arrays.toString() - 人类可读的序列化 - 配置文件
【发布时间】:2014-02-18 11:10:34
【问题描述】:

我正在构建一个配置文件,我想让它既可读又易于处理。

我只需要使用原语和原语数组(和字符串)。

这是一个例子

ID 100; Links [99, 100, 101]; Options [qwe, asd]
ID 100; Links [99, 100, 101]; Options [asd, zxc]

现在我正在使用 Arrays.toString() 和字符串连接来创建上述内容。

我可以使用String.split()分割上面的字符串,还是很简单的。

然后我剩下需要“反序列化”的 [.., ..] 字符串。有没有一种简单直接的方法来反转 Arrays.toString() 创建的内容?

【问题讨论】:

  • 您是否尝试过使用java.util.Scanner?否则我不知道任何直截了当的方式
  • 我使用扫描仪一次获取一条线,然后使用 String.split 切割碎片,而不是查看数组的每个逗号分隔值,而是做一些更聪明的事情.我目前正在查看Arrays.fill()
  • Arrays.fill 用相同的值填充所有数组单元格,恐怕你必须split 每个逗号分隔的列表并逐个转换值

标签: java arrays string serialization deserialization


【解决方案1】:

我放弃了将配置制作为简单文本文件的想法,转而使用完整的 XML。

我找到了一个不错的库,XStream,它可以生成一些冗长但可读性很强的文件。

示例输出和代码

<Person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</Person>

电话号码类

public class PhoneNumber {

    private int code;
    private String number;

    public PhoneNumber(int code, String number) {
        this.code = code;
        this.number = number;
    }

    @Override
    public String toString() {
        return "PhoneNumber{" + "code=" + code + ", number=" + number + '}';
    }

}

人物类

public class Person {

    private String firstname;
    private String lastname;
    private PhoneNumber phone;
    private PhoneNumber fax;

    public Person(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    public void setPhone(PhoneNumber phone) {
        this.phone = phone;
    }

    public void setFax(PhoneNumber fax) {
        this.fax = fax;
    }

    @Override
    public String toString() {
        return "Person{" + "firstname=" + firstname + ", lastname=" + lastname + ", phone=" + phone + ", fax=" + fax + '}';
    }

}

跑步者类

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Runner {

    private static final Logger LOG = Logger.getLogger(Runner.class.getName());

    public static void main(String[] args) {
        XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library

        // shorten tag names when saved
        xstream.alias(Person.class.getSimpleName(), Person.class);
        xstream.alias(PhoneNumber.class.getSimpleName(), PhoneNumber.class);

        // create object
        Person joe = new Person("Joe", "Walnes");
        joe.setPhone(new PhoneNumber(123, "1234-456"));
        joe.setFax(new PhoneNumber(123, "9999-999"));

        // serialize
        String xml = xstream.toXML(joe);

        // open file
        File configFile = new File("Config.txt");

        // write to file
        try (PrintWriter writer = new PrintWriter(configFile, "UTF-8");) {
            xml = xstream.toXML(joe);
            writer.println(xml);
        } catch (FileNotFoundException | UnsupportedEncodingException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }

        // read file
        try (Scanner scanner = new Scanner(configFile, "UTF-8");) {
            scanner.useDelimiter("\\A");
            xml = scanner.next();
        } catch (FileNotFoundException ex) {
            LOG.log(Level.SEVERE, null, ex);
        }

        // deserialize
        Person newJoe = (Person) xstream.fromXML(xml);

        System.out.println("Does toString return the same String? " + joe.toString().equals(newJoe.toString()));
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    相关资源
    最近更新 更多