我很困惑我怎樣才能保存我的數據,例如我hava(Student ID,Name,Surname)唯一的學生ID是Integer,我想將這些數據保存到文本文件當我按(AddButtton),然後我想當我按下(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());
}
}
}
}
爲了更快提供更好的幫助,請發佈[SSCCE](http://sscce.org/)。 –