自己练手的代码。请帮忙解决里面的问题。或者提供更好的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());
}
}
}
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());
}
}
}
//静态方法,用于将对象输出到文件中
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;
}
第二次读取问题是因为你的employee对象没有改变。
1、看视频说DataOutputStream很好用,所以对象存储到文件时首先就想到了它,而它仅有writeBytes等少数几个方法能使用String参数。
2、我在while循环外面为变量e赋予了堆内存空间,为什么每次执行完l.add(e)之后要重新分配空间呢?