小弟我定义了一个Student类,然后又定义了一集合类ArrayList,里面存放Student类型的数据
 现在小弟我想把ArrayList通过java的输出流保存为 *.txt文件中,
如何讲ArrayList保存为*.txt文件
      求高手指教》。。

解决方案 »

  1.   

    这个和你用ArrayList没多大关系,一个个取就get(i)就好,至于怎么写入txt,打开一个IO流,用你定义的格式写就好...google例子很多的
    如果你是存的自己定义的Student类,用范型吧...其实java是伪范型....取的时候直接取就好
    ArrayList<Student> arrlst = new ArrayList<Student>();
    arrlst.get(index);
      

  2.   

    给你个例子import java.io.FileNotFoundException;public class WriteData { /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) throws FileNotFoundException {
    // TODO 自动生成方法存根
    java.io.File file=new java.io.File("score.txt");
    if(file.exists()){
    System.out.println(file.getAbsolutePath());
    System.out.println("File already exists");
    System.exit(0);
    }

    //Create a file
    java.io.PrintWriter output=new java.io.PrintWriter(file);

    //Write formatted output to the file
    output.print("John T Smith ");
    output.println(90);
    output.print("Eric K Jones ");
    output.println(85);

    //Close the file
    output.close();
    }}
      

  3.   

    关键是LZ想怎么保存,是序列化保存,还是自己解析Student的属性信息来保存?
      

  4.   

    通过 stream 序列化 对象, Student 实现Serializable接口
      

  5.   


    给个土办法:
    1、Student类应该是个值对象类吧,定义:
    public String toString() {
       return 属性1 + ", " + 属性2 + ", " + 属性3 ... ;
    }2、打开目标文件:
    PrintStream out = new PrintStream(new FileOutputStream(new File("C:\\student.txt")));3、循环整个ArrayList
    out.println(student);
      

  6.   


    package com.wnf.url.test;public class Student {
    private String name;
    private int age;
    public Student(String name, int age) {
    this.name = name;
    this.age = age;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    } List<Student> stuList=new ArrayList<Student>();
    for (int i = 0; i < 100; i++) {
    Student s=new Student("stu"+i, i);
    stuList.add(s);
    }
    //**********************************************************************************
    StringBuilder sb=new StringBuilder();
    sb.append("name"+"\t"+"age");
    sb.append("\n\r");
    for (int i = 0; i < stuList.size(); i++) {
    sb.append(stuList.get(i).getName()+"\t"+stuList.get(i).getAge());
    sb.append("\n\r");
    }
    Writer writer=new FileWriter(new File("D:\\student.txt"));
    writer.write(sb.toString());
    writer.close();
    }
      

  7.   

    简单的写了个import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;public class WriteToTxt {
       public static void main(String[] args) throws IOException {
          List<Student> stuList = new ArrayList<Student>();
          Student s1 = new Student(1,"王宋",20," 西南交通大学");
          Student s2 = new Student(2,"张三",22," 北京大学");
          Student s3 = new Student(3,"李四",19," 复旦大学");
          Student s4 = new Student(4,"王五",18," 安徽工业大学");
          Student s5 = new Student(5,"赵六",22," 哈佛大学");
          stuList.add(s1);
          stuList.add(s2);
          stuList.add(s3);
          stuList.add(s4);
          stuList.add(s5);
          File studentTxt =new File("student.txt");
          PrintStream out = new PrintStream(new FileOutputStream(studentTxt));
          for(Iterator iter = stuList.iterator();iter.hasNext();) {
           String  str = ((Student)iter.next()).toString();
           byte[] buff = str.getBytes();
           out.write(buff);
           System.out.println();
          }
       }
    }class Student {
    private int id;
    private String name;
    private int age;
    private String school;

    public Student(int id,String name,int age,String school) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.school = school;
    }

    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public int getAge() {
    return age;
    }
    public void setAge(int age) {
    this.age = age;
    }
    public String getSchool() {
    return school;
    }
    public void setSchool(String school) {
    this.school = school;
    } @Override
    public String toString() {
    // TODO Auto-generated method stub
    return "编号:"+this.id+",姓名:"+this.name+",年龄:"+this.age+",在"+this.school+"读书。";
    }


    }
      

  8.   

    小弟对java的io流始终处于迷糊的状态,哪位大哥能给点总结的比较好的,条理比较的清晰的笔记分享给小弟。不胜感激!!
      

  9.   

    这个对IO的讲解:http://blog.csdn.net/gaowen_han/article/details/7163737
    http://blog.csdn.net/gaowen_han/article/details/7171893