繁体   English   中英

Java反序列化Arrays.toString()-可读序列化-配置文件

[英]Java deserialize Arrays.toString() - human readable serialization - config file

我正在构建一个配置文件,我想使其易于阅读并且易于处理。

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

这是一个例子

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

现在,我正在使用Arrays.toString()和字符串连接来创建上面的代码。

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

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

我放弃了将配置设为简单文本文件的想法,并使用了完整的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>

PhoneNumber类

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()));
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM