【问题标题】:Making Data Structure Persistent使数据结构持久化
【发布时间】:2017-07-17 07:53:14
【问题描述】:

我在 JAVA 中读取了一个文件并根据用户规范将数据转换为链接列表或树,但是如何将数据保存到文件中(作为数据结构),以便下一步当我读取数据时,我不必花费额外的精力来解析文件。

【问题讨论】:

  • 您可以使用序列化将树写入文件,然后在需要读取时反序列化。

标签: java data-structures linked-list tree


【解决方案1】:

您可以将这样的数据保存到文件中,它不是链表,但有助于理解

//save persons
public void savePersons(){
try{
    PersonsInfo p;
    String line;
    FileWriter fw= new FileWriter("input.txt");
    PrintWriter pw= new PrintWriter(fw);
    for (int i=0 ; i<persons.size();i++){
        p =(PersonsInfo)persons.get(i);
        line = p.getName() +","+ p.getAddress() +","+ p.getPhoneNum();
        pw.println(line);
    }
    pw.flush();
    pw.close();
    fw.close();
} catch (IOException ioEx){
System.out.println(ioEx);   
}   

你可以像这样检索数据,你不需要每次都解析文件

public class AddressBook{
ArrayList<PersonsInfo> persons;
public AddressBook (){
    persons = new ArrayList <PersonsInfo> ();
    loadPersons();
}
//Load Person
public void loadPersons (){
String tokens[]=null;
String name,address,phoneNum;
try{
    FileReader fr= new FileReader("input.txt");
    BufferedReader br= new BufferedReader(fr);
    String line=br.readLine();
    while (line !=null)
    {
        tokens = line.split(",");
        name=tokens[0];
        address=tokens[1];
        phoneNum=tokens[2];
        PersonsInfo p = new PersonsInfo(name,address,phoneNum);
        persons.add(p);
        line = br.readLine();
    }

br.close();
fr.close();

}catch (IOException ioEx) {
        System.out.println(ioEx);
}   
}

【讨论】:

    【解决方案2】:

    这取决于您要如何存储数据:

    • 如果您不想以人类可读的形式存储数据,那么您可以继续使用Serialization(例如here)。 Java 将在写入/读取操作期间分别负责存储/构造对象/结构。
    • 如果您想以人类可读的形式存储数据,那么您可以将数据转换为json 并以字符串格式存储。你可以使用 Jackson 的 ObjectMapper 类,例如:

      ObjectMapper mapper = new ObjectMapper(); YourClass object = new YourClass(); FileOutputStream outputStream = new FileOutputStream("path_to_file"); outputStream.write(mapper.writeValueAsBytes(object));

      //Read YourClass persisted = mapper.readValue("path_to_file", YourClass.class);

      Here's 示例和这里的 Jackson's 文档。

    【讨论】:

      【解决方案3】:

      您可以使用serialization,这是一个非常易于使用的 Java 功能:

      保存:

      ObjectOutputStream oos = null;
      try {
            LinkedList<String> list = new LinkedList<>();  
            list.add("toto");  list.add("tata");
      
            oos = new ObjectOutputStream(new FileOutputStream("C:\\Users\\..\\Doc\\list.ser"));
            oos.writeObject(list);
      } catch ... {
      }finally (oos.close) ...{
      }
      

      当然,如果不是,您可以将LinkedList 更改为您想要的任何内容


      加载:

      ObjectInputStream ois = null;
      
      try {
          ois = new ObjectInputStream(new FileInputStream("C:\\Users\\..\\Doc\\list.ser"));
      
          final LinkedList<String> list = (LinkedList<String>) ois.readObject();
          System.out.println(list.toString());   //[toto,tata]
      } catch ... {
      }finally (ois.close) ...{
      }
      

      写作时使用writeObject,阅读时使用readObject + casting 到好的类型(所以顺序很重要,如果你先写一个List,然后是String,然后是Other,你可以先读List,然后是String其他)

      Serialization Details

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        • 1970-01-01
        • 2015-07-06
        • 1970-01-01
        相关资源
        最近更新 更多