【问题标题】:Print arraylist to text file in HUMAN READABLE form以人类可读的形式将arraylist打印到文本文件
【发布时间】:2013-11-07 03:43:08
【问题描述】:

所以我不知道如何将 arraylist 索引(第一个索引为 0)打印到文本文件中。基本上,我有一个存储 5 个变量的 Job 类

public class Job {

public int teamNo;
    public String regNo;
    public String gridRef;
    public String gridCopy;

    public String toString() {

        return "Job [teamNo=" + teamNo + ", regNo=" + regNo + ", gridRef="
                + gridRef + "";

    }

然后我有一个 Job 类型的数组列表:

private static ArrayList<Job> teamNoOne = new ArrayList<Job>();

所以数据都被很好地添加,打印出来等等,但我无法将它保存到文本文件中。这是我的代码,我只是得到它的随机哈希码,但我需要它以人类可读的形式。

try {
                    File file = new File("JOBS-DONE-LOG.txt");
                    FileOutputStream fs = new FileOutputStream(file);
                    ObjectOutputStream os = new ObjectOutputStream(fs);
                    System.out.println(teamNoOne.get(0));

                    os.writeObject(teamNoOne.get(0));
                    os.close();


                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

不知道怎么做。

【问题讨论】:

    标签: java file arraylist save


    【解决方案1】:

    writeObject 序列化文件中的对象,它不会以文本形式写入 (http://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html#writeObject(java.lang.Object))

    您必须以另一种方式进行:例如,您可以使用 BufferedWriter 类和 write 方法来写入 toString() 方法的输出。

    这是一个完整的例子:

    import java.io.File;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.util.ArrayList;
    
    public class Job {
      public String regNo;
      public String gridRef;
      public String gridCopy;
    
      public String toString() {
    
        return "Job [teamNo=" + teamNo + ", regNo=" + regNo + ", gridRef="
        + gridRef + "";
    
      }
    
      public static void main(String[] args) throws Exception {
        ArrayList<Job> teamNoOne = new ArrayList<Job>();
        // fill your array
        Job job = new Job();
        job.regNo = "123";
        // continue to fill the jobs...
        teamNoOne.add(job);
        BufferedWriter writer = new BufferedWriter(new FileWriter("JOBS-DONE-LOG.txt"));
        System.out.println(teamNoOne.get(0));
        writer.write(teamNoOne.get(0).toString());
        os.close();
      }
    }
    

    【讨论】:

    • 啊,我想我明白你的意思,所以如果我试图获取 teamNoOne 数组的 toString ,它就像 (Job s : teamNoOne) { //code to打印 teamNoOne 的索引 0? }
    • 我已经用一个完整的例子更新了帖子给你一个想法。
    • 谢谢你,我已经这样实现了。它打印出来到控制台很好,但文本文件是空白的。它自上次访问更改后访问它
    • 对不起我的错误,我在我的代码中创建了两次文件,我现在更新它。现在应该更好了:-)
    • 现在完美运行!非常感谢,你是一个完整的传奇!
    【解决方案2】:

    发生这种情况是因为您没有参数化您的 ArrayList。声明列表时使用泛型:

    ArrayList<Job> teamNoOne = new ArrayList<Job>();
    

    因为现在,虽然您已经覆盖了 toString() 方法,但 teamNoOne.get(0) 使用了 ObjecttoString()

    【讨论】:

    • 对不起,我是 java 新手,有点困惑,我应该如何更改声明?由于其他依赖于它的方法,当我删除私有静态时出现错误。
    【解决方案3】:

    由于您尝试保存 Job 类型的数组列表,因此必须对其进行序列化 (Refer this)。

      public class Job implements java.io.Serializable
      {
         public int teamNo=0;
         public String regNo="default";
         public String gridRef="default";
         public String gridCopy="default";
    
        public String toString() {
    
        return "Job [teamNo=" + teamNo + ", regNo=" + regNo + ", gridRef="
                + gridRef + "";
    
        }
      }
    

    保存文件

          try
          {
             FileOutputStream fileOut = new FileOutputStream(path);
             ObjectOutputStream out = new ObjectOutputStream(fileOut);
             out.writeObject(teamNoOne);
             out.close();
             fileOut.close();
          }
          catch(IOException i)
          {
              i.printStackTrace();
          }
    

    因此您可以像这样加载数组列表

          Object o = null;
          try
          {
             FileInputStream fileIn = new FileInputStream(path);
             ObjectInputStream in = new ObjectInputStream(fileIn);
             o = in.readObject();
             in.close();
             fileIn.close();
          }
          catch(IOException i)
          {
              i.printStackTrace();
          }
          catch(ClassNotFoundException c)
          {
              c.printStackTrace();
          } 
          Arraylist<Job> loaded_Job = (ArrayList<Job>) o;
    

    然后打印数组列表

             for(int i = 0; i < loaded_Job.size(); i++) {   
                loaded_Job.get(i).toString();
              }  
    

    【讨论】:

    • 这似乎仍然不起作用。我只能将其作为输出:¬í sr Job½u™=læE§ I teamNoL gridCopyt Ljava/lang/String;L gridRefq ~ L regNoq ~ xp pt aat aa
    • 你用什么来打印?
    • 我正在使用 ObjectOutputStream 将其打印出来。我应该使用其他东西吗?
    • 我说的是打印给用户而不是保存到文件中。
    • 我不完全确定序列化后我现在该怎么做?最初我只是打算使用文件编写器
    猜你喜欢
    • 2017-01-28
    • 2015-05-05
    • 2018-08-13
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    相关资源
    最近更新 更多