最近写了一个程序 是关于学生的学生档案, 我希望从键盘输入学生的各种资料,然后保存到一个 txt的文档中(不是数据库) 可是当我把程序退出以后 再次进入这个系统时 我希望能够把写在其中我东西读出来 比如说每个学生的档案在在一行中 ( 我认为这样容易读取 ) 比如说保存在其中的学生有好多个 只是我读出来的只有最后一个学生的资料,(我希望的是读出来的东西能够在控制台看得到,) 我写入用的是 FileWriter 与BufferedWriter 的组合 读出时用的是 FileReader 与BufferedReader 的组合 (流太多了容易搞 混啊) 可是读出来就是我说的那样的情况 怎么解决啊, 是不是 还有什么方法没有用到啊, 用哪个流能够做到 (本来是指望用序列化能够做出来的 希望能够将读出来的东西放在一个学生的(Student) 的数组中 接下来的程序中还有用到 可是序列化也是刚学,更不熟,也不知道能不能搞出来) 希望能够 给我一个实例来说明这个问题, 分只给一人 也希望能给一些这方面的资料 谢谢帮忙啊
BufferedWriter
这两个类应该分别由writeLine和readLine方法的,建议你把每个学生信息写成一行,中间用个符号(比方说逗号)隔开
然后用readLine读出,
import java.io.*;
public class Test {
public static void main(String[] args) {
try {
int nLineCount = 0;//行数
File file = new File("language.txt");
BufferedReader in = new BufferedReader(new FileReader(file));
String strLine = "";
StringBuffer strBuffer = new StringBuffer(1000);
while ((strLine = in.readLine()) != null) {
strBuffer.append(strLine);
strBuffer.append("^^");
++nLineCount;
}
//最终结果保存在strResult中,第一行在strResult[0],第一行第一列在strResult[0][0]
String[][] strResult = new String[nLineCount][];
String[] strTemp = (strBuffer.toString()).split("\\^\\^");
for (int i = 0; i<strTemp.length ; ++i)
for (int j = 0 ; j<strTemp[i].length() ; ++j){
strResult[i] = strTemp[i].split(",");
}
for (int i=0 ; i<strResult.length ; ++i)
for (int j=0 ; j<strResult[i].length ; ++j){
strResult[i][j] = strResult[i][j].replaceAll("\"","");
System.out.println(strResult[i][j]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
像上面这样。就可以全读出来了。关键是读出一行来要保存到数组中去。或者边读边打出来。
模仿这个例子, 把Student类给补充完。import java.io.*;// 在Java中系列化很简单, 只要实现一个无函数的接口Serializable即可。
public class Student implements Serializable {
private static final long serialVersionUID = -6670528088216041285L;
private String name;
private int ID; public Student(String name, int ID) {
this.name = name;
this.ID = ID;
} public String getName() {
return name;
} public int getID() {
return ID;
} public static void main(String[] agrs) {
// 先产生几个学生的对象
Student st1 = new Student("Google", 100);
Student st2 = new Student("Baidu", 101);
Student st3 = new Student("Yahoo", 102); // 把对象写入文件.
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("data.txt"));
oos.writeObject(st1);
oos.writeObject(st2);
oos.writeObject(st3); st1 = null;
st2 = null;
st3 = null;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
oos = null;
} catch (IOException e) {
e.printStackTrace();
}
}
} // 把对象从文件读入.
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("data.txt"));
st3 = (Student) ois.readObject();
st2 = (Student) ois.readObject();
st1 = (Student) ois.readObject(); // 输出从文件中读取到的学生的信息, 用于查检是否正确
System.out.println(st1.getName() + "'s ID is " + st1.getID());
System.out.println(st2.getName() + "'s ID is " + st2.getID());
System.out.println(st3.getName() + "'s ID is " + st3.getID());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
ois = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 这个类用来记录一条记录的信息
* @author Teng MA
*
*/
public class Record implements Serializable{//主要要序列化该类
String field1;
String field2;
String filed3;
public Record(String field1, String field2, String filed3) {
super();
this.field1 = field1;
this.field2 = field2;
this.filed3 = filed3;
}
}import java.io.*;
import java.util.ArrayList;/**
* 这个类用来测试对象读写
* @author Teng MA
*
*/
public class Test {
/**
* database 用来存放每次从文件读入的记录
*/
ArrayList<Record> database = null;
File file = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null; public Test() throws FileNotFoundException, IOException {
database = new ArrayList<Record>();
file = new File("test.txt");
//以追加的方式向文件中添加记录
oos = new ObjectOutputStream(new FileOutputStream(file, true));
ois = new ObjectInputStream(new FileInputStream(file));
} public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
Test t = new Test();
//测试向文件写入3条记录
t.testWrite();
//测试载入文件记录
t.testRead();
}
//向文件写3条记录
public void testWrite() throws FileNotFoundException, IOException {
Record r = new Record("1field1", "1field2", "1field3");
addARecord(r);
Record r2 = new Record("2field1", "2field2", "2field3");
addARecord(r2);
Record r3 = new Record("3field1", "3field2", "3field3");
addARecord(r3);
}
public void testRead() {
LoadFile();
Record r = null;
for (int i = 0; i < database.size(); i++) {
r = database.get(i);
System.out.println("===============================");
System.out.println(showARecord(r));
}
}
//输出一条记录
public String showARecord(Record r) {
StringBuffer strBuffer = new StringBuffer(1000);
strBuffer.append("field1: " + r.field1 + "\n");
strBuffer.append("field2: " + r.field2 + "\n");
strBuffer.append("field3: " + r.filed3 + "\n");
return strBuffer.toString();
}
//载入文件
public void LoadFile() {
while (true) {
Object obj = null;
try {
obj = ois.readObject();
} catch (Exception e) {
break;
} if (obj != null) {
database.add((Record) obj);
} else {
break;
}
}
} public void addARecord(Record r) throws FileNotFoundException, IOException {
oos.writeObject(r);
oos.flush();
}
}
public void sturead() throws IOException,ClassNotFoundException{
fos=new FileInputStream("student.txt");
oo=new ObjectInputStream(fos);
for(int i=0;i<2;i++){
c[i]=(student)oo.readObject();
}
oo.close(); fos.close();
for(int j=1;j>=0;j--){
System.out.println(c[j].getstunum()+c[j].getname()+c[j].getsex()+c[j].getage()+c[j].getjava()+c[j].getc()+c[j].getenglish());
print.p(c[j].getname());
}如果我有很多的学生要输入 那怎么办啊, 要像二楼的那样一个个的都把对象都写出来 那不是麻烦死了啊,