如何写入和检索对象的ArrayList到文件? [英] How to write and retrieve objects arraylist to file?

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

问题描述

我有一个对象数组列表,可有人请告诉我,写和检索文件中的对象最有效的方式帮助我吗?

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

我尝试检索文件。此外,检索文件之后,我怎么能阅读对象的每列?举例来说,如果我有:::::小说,戏剧,戏剧---我怎样才能阅读,获取,替换,删除和添加值共赏列?

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).

下面两个重要讲话:

连载

内部Java序列化


  • Java提供了自动序列这就要求对象通过实现java.io.Serializable接口进行标记。实现该接口标志着类好序列化和Java然后在内部处理序列化。

  • 请参见为code样品这篇文章如何序列化/
    从字节反序列化对象/

  • Java provides automatic serialization which requires that the object be marked by implementing the java.io.Serializable interface. Implementing the interface marks the class as "okay to serialize," and Java then handles serialization internally.
  • See this post for a code sample on how to serialize / deserialize an object to/from bytes.

这可能NOG始终坚持一个对象的理想方式,因为你有过(被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


  • 一个更好的办法来seralize对象磁盘是用另一种数据格式,如XML或JSON。

  • 在如何将对象从JSON结构转换成/ A样本可以发现的这里

  • A better way to seralize an object to disk is to use another data format like XML or JSON.
  • A sample on how to convert an object to/from a JSON structure can be found here.

重要:我不喜欢你这样做,除非有很好的理由(即我没有看到这里)做那种在code系列化。它很快变得混乱,并须当你的对象,改改。我会选择序列化更自动化的方式。此外,使用诸如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用很少的code,以保存对象。
如果它是一个文档数据库MongoDB的一样,你可以坚持你的JSON序列重新presentation。

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 的标准API进行持久化和对象/关系映射。

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 .

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

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