-1

I already have methods to insert data in to a text file and search. now I want methods for 'updating' and 'deleting' a selected record.

this is what I have done so far,

private void InsertbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    fileWriter = null;
    try {
        String phone = Phone.getText();
        String fname = Fname.getText();
        String lname = Lname.getText();
        String nic = NIC.getText();
        String city = City.getSelectedItem().toString();

        fileWriter = new FileWriter(file, true);
        fileWriter.append(phone + "|" + fname + "|" + lname + "|" + nic + "|" + city+ "|");
        fileWriter.append("\r\n");
        fileWriter.flush();
        JOptionPane.showMessageDialog(InsertGUI.this, "<html> " + phone + " <br> Successfully saved! </html>");

        Phone.setText(null);
        NIC.setText(null);
        Fname.setText(null);
        Lname.setText(null);
        City.setSelectedIndex(0);

    } catch (IOException ex) {
        Logger.getLogger(InsertGUI.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            fileWriter.close();
        } catch (IOException ex) {
            Logger.getLogger(InsertGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


private void searchbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                             

    try {
        fileReader = new FileReader(file);
        BufferedReader br = new BufferedReader(fileReader);

        String selectedphone = SearchF.getText();

        String content = "";
        String temp = "";
        try {
            while ((temp = br.readLine()) != null) {
                content += temp;
            }
        } catch (IOException ex) {
            Logger.getLogger(UpdateGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        HashMap<String, String> map = new HashMap();
        StringTokenizer stringTokenizer = new StringTokenizer(content, "|");
        boolean found = false;
        while (stringTokenizer.hasMoreTokens()) {
            String phone = stringTokenizer.nextElement().toString();
            String fname = stringTokenizer.nextElement().toString();
            String lname = stringTokenizer.nextElement().toString();
            String nic = stringTokenizer.nextElement().toString();
            String city = stringTokenizer.nextElement().toString();

            if (phone.equalsIgnoreCase(selectedphone)) {

                Phone.setText(phone);
                NIC.setText(nic);
                Fname.setText(fname);
                Lname.setText(lname);
                switch (city) {
                    case "Ambalangoda":
                        City.setSelectedIndex(0);
                        break;
                    case "Ampara":
                        City.setSelectedIndex(1);
                        break;
                    case "Anuradhapura":
                        City.setSelectedIndex(2);
                        break;
                    case "Avissawella":
                        City.setSelectedIndex(3);
                        break;
                    case "Badulla":
                        City.setSelectedIndex(4);
                        break;
                    case "Balangoda":
                        City.setSelectedIndex(5);
                        break;
                    }
                found = true;

            }

        }
        if (!found) {
            JOptionPane.showMessageDialog(UpdateGUI.this, "Phone number not found!");
            Phone.setText(null);
            NIC.setText(null);
            Fname.setText(null);
            Lname.setText(null);
            City.setSelectedIndex(0);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(UpdateGUI.class.getName()).log(Level.SEVERE, null, ex);
    }
}

can someone please help me with this? I want methods for:

private void UpdatebuttonActionPerformed(java.awt.event.ActionEvent evt) {

}
private void DeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {                                             

}

Thanks in advance! :)

P. K. Tharindu
  • 2,565
  • 3
  • 17
  • 34
  • http://stackoverflow.com/questions/1377279/find-a-line-in-a-file-and-remove-it – Pandiyan Cool Jun 22 '15 at 13:06
  • This is answered [Here - Find a line and remove it](http://stackoverflow.com/questions/1377279/find-a-line-in-a-file-and-remove-it) or [Here - delete using canner](http://stackoverflow.com/questions/20048633/how-to-delete-a-line-from-a-text-file-with-java-using-scanner) and [Update-here](http://stackoverflow.com/questions/20039980/java-replace-line-in-text-file) – Kenneth Clark Jun 22 '15 at 13:07
  • You must read through the entire file, writing each line to a new file, changing or omitting the relevant line. Then you can copy the new file on top of the old one. – VGR Jun 22 '15 at 14:21

2 Answers2

1

I give you an example that i found. In this example i will replace a string inside of a line. You can see that in my case im reading from a txt.

/******

public static void replaceSelected(String replaceWith, String type) {
    try {
        // input the file content to the String "input"
        BufferedReader file = new BufferedReader(new FileReader("notes.txt"));
        String line;String input = "";

        while ((line = file.readLine()) != null) input += line + '\n';

        file.close();

        System.out.println(input); // check that it's inputted right

        // this if structure determines whether or not to replace "0" or "1"
        if (Integer.parseInt(type) == 0) {
            input = input.replace(replaceWith + "1", replaceWith + "0"); 
        }
        else if (Integer.parseInt(type) == 1) {
            input = input.replace(replaceWith + "0", replaceWith + "1");
        } 

        // check if the new input is right
        System.out.println("----------------------------------"  + '\n' + input);

        // write the new String with the replaced line OVER the same file
        FileOutputStream fileOut = new FileOutputStream("notes.txt");
        fileOut.write(input.getBytes());
        fileOut.close();

    } catch (Exception e) {
        System.out.println("Problem reading file.");
    }
}

public static void main(String[] args) {
    replaceSelected("Do the dishes","1");    
}

*/

Result:

Original:

Original Text File Content:

Do the dishes0 Feed the dog0 Cleaned my room1 Output:

Do the dishes0 Feed the dog0

Cleaned my room1

Do the dishes1 Feed the dog0 Cleaned my room1


Recent output:

Do the dishes1 (HAS BEEN CHANGED) Feed the dog0 Cleaned my room1


Maybe this example helps for you.

Regards!

carlos gil
  • 139
  • 3
  • Using `+=` on a String in a loop is something code should never do. Use StringBuilder. Also, reading the entire file into memory will not work well with large files. – VGR Jun 22 '15 at 13:35
0

Read data to string. Use string.replace(forDelte,""). And then just rewrite to txt file.

Uroš Hrastar
  • 69
  • 1
  • 7