java io 写不进数据,读取一直是null

 @SuppressWarnings("unchecked")
public void reader() throws IOException, ClassNotFoundException {
          file = new File("src/com/lzf/www/dao/1.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            ois = new ObjectInputStream(new FileInputStream(file));
            list = (List<Employee>) ois.readObject(); // 给list赋值
            ois.close();

        }

        /**
         * 写文件
         * @throws IOException
         * @throws ClassNotFoundException
         */
        public void writer() throws IOException, ClassNotFoundException {
            file = new File("src/com/lzf/www/dao/1.txt");
            if (!file.exists()) {
                file.createNewFile();
            }
            oos = new ObjectOutputStream(new FileOutputStream(file));

            for(Employee e:list){
                oos.writeObject(e.toString()+"\r\n"); // 将list值写进文件里去
            }        
            oos.flush();
            oos.close();
        }

file = new File("src/com/lzf/www/dao/1.txt");
if (!file.exists()) {
file.createNewFile();
}

读取不到文件就自己创建,创建的内容怎么可能不为空?一般是需要确定file不为空,读取到文件,如果找不到文件,直接抛异常即可。

有可能是,对象使用toString时,一般需要进行重写。http://blog.163.com/quanquan127@126/blog/static/688477252012514598901/

不是,是写进去的值为空,是不是toString()的问题

package com.example.csdn;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Test1 implements Serializable{

private static final long serialVersionUID = -2320329677898640646L;

public void reader() throws IOException, ClassNotFoundException {
    File file = new File("D:/1.txt");
      if (!file.exists()) {
          file.createNewFile();
      }
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));

      Employee e = (Employee) ois.readObject();
      Employee e2 = (Employee) ois.readObject();
      System.out.println(e.getName());
      System.out.println(e2.getName());

// List list = (List) ois.readObject(); // 给list赋值

      ois.close();

  }

public void writer() throws IOException, ClassNotFoundException {
    File file = new File("D:/1.txt");
    if (!file.exists()) {
        file.createNewFile();
    }
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));

    List<Employee> list = new ArrayList<Employee>();
    list.add(new Employee("123","abc"));
    list.add(new Employee("456","edf"));

    for (Employee e : list) {
        oos.writeObject(e);

// oos.writeObject(e.toString() + "\r\n"); // 将list值写进文件里去
}
oos.flush();
oos.close();
}

class Employee implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 3323412483202557661L;
    private String id;
    private String name;

    public Employee(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public static void main(String[] args) throws ClassNotFoundException, IOException {
    new Test1().writer();
    new Test1().reader();
}

}
图片说明


换一个字节流,不要使用objectoutputstream,直接用OutPutStream