This discussion is archived
2 Replies Latest reply: Oct 27, 2008 9:16 AM by 807589 RSS

UPDATING AND DELETING DATA IN A TEXTFILE

807589 Newbie
Currently Being Moderated
Hi Guys,

I am using textfiles a database to store data for a travel agent and have ran into a problem with updating the textfile with out replace the entire data in the file. I am using StringTokenizer to read and place the relevant data in the correct textField so heres the problem if I try to update a field the entry select is updated and the other entries are deleted. Heres part of my code:

code1 to add entries:

if(event.getSource() == addButton) {
                         
                    try {
                    
                    outFile1 = new PrintWriter ( new FileWriter("flight.txt",true));
                    outFile1.print(cityField.getText()+", ");
                    outFile1.print(departdateField.getText()+", ");
                    outFile1.print(departtimeField.getText()+", ");
                    outFile1.print(returndateField.getText()+", ");
                    outFile1.print(returntimeField.getText()+", ");
                    outFile1.print(priceField.getText()+", ");
                    outFile1.print(referenceField.getText()+", " + "\r\n");     
                    
                    outFile1.flush();
                    outFile1.close();
                    JOptionPane.showMessageDialog(null,"Data have been Saved");
                    
                    cityField.setText("");
                    departdateField.setText("" );
                    departtimeField.setText("");
                    returndateField.setText("");
                    returntimeField.setText("");
                    priceField.setText("");
                    referenceField.setText("");
                                        
     }
               
               catch (IOException e) {
                    
                    JOptionPane.showMessageDialog(null,"Search Not Found");
                    
                    return;
     }
               
     
}     

code2 update entries:

if(event.getSource()==updateButton){
          
               
                    try {
                    
                    outFile1 = new PrintWriter ( new FileWriter("flight.txt"));
                    outFile1.print(cityField.getText()+", ");
                    outFile1.print(departdateField.getText()+", ");
                    outFile1.print(departtimeField.getText()+", ");
                    outFile1.print(returndateField.getText()+", ");
                    outFile1.print(returntimeField.getText()+", ");
                    outFile1.print(priceField.getText()+", ");
                    outFile1.print(referenceField.getText()+", ");     
                    
                    outFile1.flush();
                    outFile1.close();
                    JOptionPane.showMessageDialog(null,"Data have been Saved");
                    
                    cityField.setText("");
                    departdateField.setText("" );
                    departtimeField.setText("");
                    returndateField.setText("");
                    returntimeField.setText("");
                    priceField.setText("");
                    referenceField.setText("");
                                        
     }
               
               catch (IOException e) {
                    
                    JOptionPane.showMessageDialog(null,"Search Not Found");
                    
                    return;
     
     }
     }

Can anyone help!!
  • 1. Re: UPDATING AND DELETING DATA IN A TEXTFILE
    807589 Newbie
    Currently Being Moderated
    You can't "edit" a section of a text file easily. What's wrong with writing out the entire contents?
  • 2. Re: UPDATING AND DELETING DATA IN A TEXTFILE
    807589 Newbie
    Currently Being Moderated
    I think there are ways to open a file for appending, and maybe using Random Access File you can remove / change certain parts of the file. But honestly these days file data throughput is very very high (certainly 100s of megabytes / sec, maybe gigabytes / sec), but file latency (the time it takes for the HD to get to the right sector) is the key performance stopper. So writing the whole file to disk again doesn't cost you anything. I rewrite the whole file with all of my changing files.