如何将对象数组列表写入和检索到文件? [英] How to write and retrieve objects arraylist to file?

查看:25
本文介绍了如何将对象数组列表写入和检索到文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组列表,有人可以告诉我从文件中编写和检索对象的最有效方法吗?

I have an object arraylist, can someone please help me by telling me the most efficient way to write AND retrieve an object from file?

谢谢.

我的尝试

 public static void LOLadd(String ab, String cd, int ef) throws IOException {
     MyShelf newS = new MyShelf();
     newS.Fbooks = ab;
     newS.Bbooks = cd;
     newS.Cbooks = ef;
     InfoList.add(newS);

         FileWriter fw;
                 fw = new FileWriter("UserInfo.out.txt");
             PrintWriter outt = new PrintWriter(eh);
                 for (int i = 0; i <InfoList.size(); i++)
                 {
                     String ax = InfoList.get(i).Fbooks;
                     String ay = InfoList.get(i).Bbooks;
                     int az = InfoList.get(i).Cbooks;
                     output.print(ax + " " + ay + " " + az);  //Output all the words to file // on the same line
                     output.println(""); //Make space
                 }
                 fw.close();
                 output.close();
}

我尝试检索文件.另外,检索文件后,如何读取对象的每一列??例如,如果我有 ::::: Fictions, Dramas, Plays --- 我如何读取、获取、替换、删除和添加值到戏剧列?

My attempt to retrieve file. Also, after retrieving file, how can I read each column of Objects?? For example, if I have ::::: Fictions, Dramas, Plays --- How can I read, get, replace, delete, and add values to Dramas column?

public Object findUsername(String a) throws FileNotFoundException, IOException,     ClassNotFoundException  
{    
 ObjectInputStream sc = new ObjectInputStream(new FileInputStream("myShelf.out.txt"));

    //ArrayList<Object> List = new ArrayList<Object>();
    InfoList = null;
    Object  obj = (Object) sc.readObject();

    InfoList.add((UserInfo) obj);
    sc.close();

     for (int i=0; i <InfoList.size(); i++) {
          if (InfoList.get(i).user.equals(a)){
               return "something" + InfoList.get(i);
      }
   }
     return "doesn't exist"; 
 }

public static String lbooksMatching(String b)    {

    //Retrieve data from file
    //...

    for (int i=0; i<myShelf.size(); i++)       {
        if (myShelf.get(i).user.equals (b))   
        {
            return b;
        }
        else 
        {
            return "dfbgregd"; 
        }
   }
    return "dfgdfge"; 
}

public static String matching(String qp)    {
    for (int i=0; i<myShelf.size(); i++)       {
        if (myShelf.get(i).pass.equals (qp))   
        {
            return c;
        }
        else 
        {
            return "Begegne"; 
        }
   }
    return "Bdfge"; 
}

谢谢!!!

推荐答案

似乎您想要序列化一个对象并将该序列化形式持久化到某种存储(在本例中为文件).

It seems like you want to serialize an object and persist that serialized form to some kind of storage (in this case a file).

这里有两个重要的说明:

2 important remarks here :

序列化

内部java序列化

  • Java 提供自动序列化,它要求通过实现 java.io.Serializable 接口来标记对象.实现接口将类标记为可以序列化",然后 Java 在内部处理序列化.
  • 有关如何序列化/将对象反序列化为字节/从字节反序列化.

这可能始终是持久化对象的理想方式,因为您无法控制格式(由 Java 处理),它不是人类可读的,并且如果您的对象发生变化,您可能会出现版本问题.

This might nog always be the ideal way to persist an object, as you have no control over the format (handled by java), it's not human readable, and you can versioning issues if your objects change.

编组为 JSON 或 XML

  • 将对象序列化到磁盘的更好方法是使用另一种数据格式,如 XML 或 JSON.
  • 可以找到有关如何将对象转换为 JSON 结构的示例此处.

重要提示:除非有很好的理由(我在这里没有看到),否则我不会像您那样在代码中进行序列化.它很快就会变得凌乱,并且随着对象的变化而变化.我会选择更自动化的序列化方式.此外,在使用 JSON/XML 等格式时,您知道有大量 API 可用于读取/写入该格式,因此您不再需要实现所有这些序列化/反序列化逻辑.

Important : I would not do the kind of serialization in code like you're doing unless there is a very good reason (that I don't see here). It quickly becomes messy and is subject to change when your objects change. I would opt for a more automated way of serializing. Also, when using a format like JSON / XML, you know that there are tons of APIs available to read/write to that format, so all of that serialization / deserialization logic doesn't need to be implemented by you anymore.

坚持

由于各种原因(没有版本控制/并发问题/.....),将序列化对象写入文件并不总是一个好主意.

Writing your serialized object to a file isn't always a good idea for various reasons (no versioning / concurrency issues / .....).

更好的方法是使用数据库.如果它是分层数据库,请查看 Hibernate 或 JPA 以使用很少的代码持久化您的对象.如果是像 MongoDB 这样的文档数据库,你可以持久化你的 JSON 序列化表示.

A better approach is to use a database. If it's a hierarchical database, take a look at Hibernate or JPA to persist your objects with very little code. If it's a document database like MongoDB, you can persist your JSON serialized representation.

有大量资源可用于在 Java 中将对象持久化到数据库.我建议查看 JPA,这是用于持久性和对象/关系映射.

There are tons of resources available on persisting objects to databases in Java. I would suggest checking out JPA, the the standard API for persistence and object/relational mapping .

这篇关于如何将对象数组列表写入和检索到文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆