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

    }


  }
  • When asking for free advice, we appreciate it greatly if you put the effort into posting code that is well formatted and easy to read. After all, you're asking for effort from volunteers, so it's not too much to ask for you to put in some effort so that our work is not more difficult than it needs to be. Your code indentation for instance is all over the place. What's with that? – Hovercraft Full Of Eels Jan 12 '13 at 14:21
  • Also, you're asking about writing to a *RandomAccessFile*, but your question mentions a *text* file, and your code shows *neither*. Now I'm quite confused. – Hovercraft Full Of Eels Jan 12 '13 at 14:25
  • So sorry,this is my mistake I solved this project by two ways but really should be put random access file part not this – user1883699 Jan 12 '13 at 14:34
  • if I talking about this why the integer number don't show in the text file – user1883699 Jan 12 '13 at 14:35
  • Then please edit your post, tell us how you've "solved" this, tell us if you still have issues, and clearly explain what the issues are, only show ***clean well-formatted code***, and remove all mention of RandomAccessFile, especially in your thread title. – Hovercraft Full Of Eels Jan 12 '13 at 14:35
  • when I run a program the out put appear like this [ 2 0 1 2 S t u d e n t T e c h e r ] and i wand to save as a record and with out braket – user1883699 Jan 12 '13 at 15:15

2 Answers2

1

You should use the constructor FileOutputStream(File file, boolean append) which is able to initialize the stream to the file in append mode, thus appending data past the end of the file.

Jack
  • 131,802
  • 30
  • 241
  • 343
0

You shouldn't use a RandomAccessFile to store text. Instead it should be used to store equal length records. If all you want to do is append text to the end of a text file, then do as @Jack recommends in his answer -- append to the File (1+ to him).

Myself, I'd use a FileWriter with the constructor's append boolean parameter set to true (similar to how Jack shows you) and would wrap this in a BufferedWriter, perhaps wrapped by a PrintWriter. Then you could write new lines using println(...).

Also, the brackets are likely in your output because you are telling them to be there. What does the toString() method for Student look like? Something tells me it outputs the brackets. Get rid of them from that method.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373