以人类可读形式将arraylist打印到文本文件

时间:2013-10-28 11:28:44

标签: java file arraylist save

所以我无法弄清楚如何将arraylist索引(第一个索引为0)打印到文本文件中。基本上,我有一个Job类,它存储了5个变量

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类型的arraylist:

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

无法弄清楚如何去做。

3 个答案:

答案 0 :(得分: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();
  }
}

答案 1 :(得分:0)

由于您正在尝试保存Job类型的arraylist,因此必须将其序列化(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();
      }

因此你可以像

一样加载arraylist
      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;

然后打印arraylist

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

答案 2 :(得分:-2)

这是因为您没有参数化ArrayList。在声明列表时使用泛型:

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

因为现在,虽然您已覆盖了toString()方法,但teamNoOne.get(0)使用了Object的{​​{1}}。