-1

所以我不知道如何将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();
                    }

无法弄清楚该怎么做。

4

3 回答 3

1

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

您必须以另一种方式进行:例如,您可以使用 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();
  }
}
于 2013-10-28T11:34:07.043 回答
0

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

  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;

然后打印arraylist

         for(int i = 0; i < loaded_Job.size(); i++) {   
            loaded_Job.get(i).toString();
          }  
于 2013-10-28T11:38:55.207 回答
-2

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

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

因为现在,尽管您已经覆盖了您的toString()方法,但teamNoOne.get(0)使用了Object's toString()

于 2013-10-28T11:34:35.343 回答