2

I'm confused how can I save my data for example I hava (Student ID , Name , Surname) the only Student ID is Integer, I want save these data to text file when I Press (AddButtton) and then I want to update these data when I press (UpdateButton),the data are saved but I'm used Vector Array each time when the data saved the brackets are appear in the file and when press the (UpdateButton) the data aren't return to my TextField to update.

     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() );
            }
        }

    }


  }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
salih
  • 21
  • 3

2 Answers2

2

I would recommend you use a BufferedWriter if you're not creating or updating that many records. If you are, I'd suggest you use a database like Mongo or MySQL

And, you might also want to start testing your code with UTF-8 data. Umlauts and such. :)

Will
  • 6,179
  • 4
  • 31
  • 49
0

I suggest you write a small amount of code you understand before writing any more. The more code you write this you are confused about the harder it is to fix.

I would start again writing another class which you unit test start with the simplest possible code you can write and when this works add a little more and test that.

Unfortunately some of the code you have written is just nonsense which is why I wouldn't reuse any of it.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • I wouldn't edit the code because "I wouldn't reuse any of it." You need to learn how to write code you understand, which will not be helped by me writting it for you. The code you have written appears to have a quilt of code you have copied which is why you are in this mess to start with. I will help you write the code and write unit tests but writing it for you would just make things worse for you in the long run. – Peter Lawrey Jan 13 '13 at 14:32
  • no, i wrote the code myself and i told you edit to me not write the program to me – salih Jan 13 '13 at 15:09