我很困惑如何保存我的数据,例如我有(学生 ID,姓名,姓氏)唯一的学生 ID 是整数,我想在按下(添加按钮)时将这些数据保存到文本文件,然后我想更新这些按下(UpdateButton)时的数据,数据被保存,但每次保存的数据出现在文件中时,我都使用向量数组,当按下(UpdateButton)时,数据不会返回到我的TextField进行更新.
class BtnListenerAdd implements ActionListener{
public void actionPerformed(ActionEvent a) {
if (a.getSource()==btnAdd){
Student s=new Student(tfname.getText(),tfsurname.getText());
s.number=Integer.parseInt(tfno.getText());
myVector.add(s);
//System.out.println(s);
try{
java.io.RandomAccessFile raf =
new java.io.RandomAccessFile("e:\\random.txt", "rw");
for (int i = 0; i < myVector.size(); i++) {
raf.writeChars(myVector.toString());
}
raf.seek(0);
while (raf.getFilePointer() < raf.length()) {
System.out.println(raf.readChar());
}
raf.close();
}catch(Exception e){
System.out.println( e.toString() );
}
}
if (a.getSource()==btnList){
ta1.setText("");
for (Student s : myVector) {
ta1.append(s.toString()+ "\n");
}
}
}
}
class BtnListenerUpdate implements ActionListener{
public void actionPerformed(ActionEvent s) {
if (s.getSource()==btnUpdate){
Student l=new Student(tfname.getText(),tfsurname.getText());
l.number=Integer.parseInt(tfno.getText());
myVector.add(l);
try{
java.io.RandomAccessFile raf =
new java.io.RandomAccessFile("e:\\random.txt", "rw");
for(int i=0; i <myVector.size(); i++) {
raf.writeChar(myVector.size());
}
raf.seek(0);
int no= raf.readInt();
tfno.setText(no + "");
System.out.println(no);
String nam = "";
for(int x=0; x<50; x++){
nam += raf.readChar();
}
nam = "";
for(int x=0; x<50; x++){
nam += raf.readChar();
}
}catch(Exception e){
System.out.println( e.toString() );
}
}
}
}