自己练手的代码。请帮忙解决里面的问题。或者提供更好的Object和File存储读取方法吧~~~import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;class employee{
int id;
String name;
double wage;
//构造方法
employee(){
id=0;
name="Unknown";
wage=0;
}
employee(int id,String name,double wage){
this.id=id;
this.name=name;
this.wage=wage;
}
//重写toString方法,静态的FileOutput调用了toString方法
public String toString(){
return id+"\t"+name+"\t"+wage+"\r\n";
}
//静态方法,用于将对象输出到文件中
static public void FileOutput(String s,employee e){
try {
//File输出+Data输出流
FileOutputStream fos=new FileOutputStream(s,true);
DataOutputStream dos=new DataOutputStream(fos);
//System.out.println(e);
//为什么中文在文件中为乱码呢???
dos.writeBytes(e.toString());
//关闭资源
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
System.exit(-1);
}
}
//静态方法,用于从文件中读出对象到List容器中
static public List<employee> FileInput(String s){
List<employee> l=new ArrayList<employee>();
employee e=new employee();
String getLine=null;
String [] temp=new String[3];
try {
//File+Buffer输入流
FileReader fis=new FileReader(s);
BufferedReader br=new BufferedReader(fis);
//将文件读入到List容器内,如果toString方法修改,则该处也需修改
while((getLine=br.readLine()) !=null){
temp=getLine.split("\t");
e.id=Integer.parseInt(temp[0]);
e.name=temp[1];
e.wage=Double.parseDouble(temp[2]);
l.add(e);
//为什么加了后面这句话之后,内容读取就正常了呢???
e=new employee();
}
br.close();
} catch (IOException e2) {
e2.printStackTrace();
System.exit(-1);
}
return l;
}
}public class TestFileOutput { /**
 * @param args
 */
public static void main(String[] args) {
employee e1=new employee(10,"zhangsan",4200.25);
employee.FileOutput("e:/java/11.txt",e1);
employee e2=new employee(20,"大刀王五",4200.40);
employee.FileOutput("e:/java/11.txt",e2);
System.out.println("已输出!");
List<employee> l1;
l1=employee.FileInput("e:/java/11.txt");
Iterator<employee> it=l1.iterator();
while(it.hasNext()){
System.out.print(it.next());
}
}
}

解决方案 »

  1.   


      //静态方法,用于将对象输出到文件中
        static public void FileOutput(String s,employee e){
            try {
             Writer writer=new FileWriter(s,true);
             writer.write(e.toString());
             writer.close();
            } catch (IOException e1) {
                e1.printStackTrace();
                System.exit(-1);
            }
        }
       
        //静态方法,用于从文件中读出对象到List容器中
        static public List<employee> FileInput(String s){
            List<employee> l=new ArrayList<employee>();
            employee e;
            String getLine=null;
            String [] temp=new String[3];
            try {
                //File+Buffer输入流
                FileReader fis=new FileReader(s);
                BufferedReader br=new BufferedReader(fis);
                //将文件读入到List容器内,如果toString方法修改,则该处也需修改
                while((getLine=br.readLine()) !=null){
                 e=new employee();
                    temp=getLine.split("\t");
                    e.id=Integer.parseInt(temp[0]);
                    e.name=temp[1];
                    e.wage=Double.parseDouble(temp[2]);
                    l.add(e);
                }
                br.close();    
            } catch (IOException e2) {
                e2.printStackTrace();
                System.exit(-1);
            }
            return l;
        }
      

  2.   

    第一个乱码是因为你用的是writeBytes()方法。
    第二次读取问题是因为你的employee对象没有改变。
      

  3.   

    按你说的修改了一下,OK了。能详细说一下原因吗?
    1、看视频说DataOutputStream很好用,所以对象存储到文件时首先就想到了它,而它仅有writeBytes等少数几个方法能使用String参数。
    2、我在while循环外面为变量e赋予了堆内存空间,为什么每次执行完l.add(e)之后要重新分配空间呢?
      

  4.   

    1.API上说的很清楚,DataOutputStream 数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。也就是说这个输出流主要是为形如int、double等基本类型提供使用的。至于你说的看视频说DataOutputStream很好用估计你是曲解了视频的意思。再看writeBytes方法,是将字符串按字节(而非字符)顺序写出到基础输出流中,所以会出现你说的中文乱码。2.按照你那种声明,每次执行while循环, 都没有执行employee e=new employee(),所以这个指向的内存一直没变,也就导致你的获得对象e一直不变。所以说用到while循环的时候是在while循环里面声明Object对象的。