Results 1 to 7 of 7
  1. #1
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Method that reads text files and deletes a specific string?

    Hey.

    I'm not very good with java, or programming in general.
    I started writing this small java program for my friend and got stuck while trying to add an feature into it.
    The program is very simple, it saves the data into a text file you enter into the JTextFields.
    Now I'm trying to make it search for the data you enter into a textfield from specific .txt files and delete the matching line(s).
    I almost got it working, the only problem is that it deletes all lines from the .txt document, not just the one it should.
    I'm using the swing stuff, heres a screenshot maybe it explains more:
    http://i451.photobucket.com/albums/q...ehandlerss.png

    I would appreciate if you check the code for me.
    I copy pasted the lines that has anything to do with the method:


    ActionListener:
    Java Code:
            searchitembutton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    searchitembuttonActionPerformed(evt);
                }
            });

    ActionPerformed:
    Java Code:
        public void searchitemActionPerformed(java.awt.event.ActionEvent e) throws IOException {
            searchitemstring = (searchitemtextfield.getText());
            replace(searchitemstring, newstring, in, out);
        }

    Method replace:
    Java Code:
        public void replace(String searchitemstring, String newstring, BufferedReader in, BufferedWriter out)
        throws IOException {
        newstring = "";
        searchitemstring = (searchitemtextfield.getText());
        BufferedReader reader = new BufferedReader(new FileReader("Amulets.txt"));
        PrintWriter writer = new PrintWriter(new FileWriter("Amulets.txt"));
        String line = null;
        while ((line = reader.readLine()) != null)
            writer.println(line.replace(searchitemstring,newstring));
        	reader.close();
        	writer.close();
        	searchitemtextfield.setText("");
        return;
    }

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Method that reads text files and deletes a specific string?

    wrap the following code:
    Java Code:
    writer.println(line.replace(searchitemstring, newstring));
    with some IF clause to only write to the file if you find the string first

    the idea is so that you only write on that line, not all the lines you are looping through

    you could also break the loop after you've written to the file.

    you are looking for something like this:

    Java Code:
    while ((read the lines) is not null) {
       if (we find what we are looking for) {
           write over that line;
           break;
       }
    }
    close file readers

  3. #3
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Cool Re: Method that reads text files and deletes a specific string?

    Ok, thanks for your time.
    I actually have no clue how I can check if the reader finds the line we're looking for so this is what I came up with:

    Java Code:
        public void replace(String searchitemstring, String newstring, BufferedReader in, BufferedWriter out)
        throws IOException {
        newstring = "";
        searchitemstring = (searchitemtextfield.getText());
        BufferedReader reader = new BufferedReader(new FileReader("Amulets.txt"));
        PrintWriter writer = new PrintWriter(new FileWriter("Amulets.txt"));
        String line = null;
        while ((line = reader.readLine()) != null)
        	if (reader.equals(searchitemstring)) {
            writer.println(line.replace(searchitemstring,newstring));
        	break;
        	}
        	reader.close();
        	writer.close();
        	searchitemtextfield.setText("");
        return;
    }

    And as you probably already know, it does not work.
    The method still deletes all the lines in the text document, not just the one we're looking for.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Method that reads text files and deletes a specific string?

    Try reading from one file and writing to another. In this case you *will* have to replace and write each line. Once the file has been successfully written delete the old file and rename the new one.

    The idea is to avoid reading and writing from the same file.

  5. #5
    Mike. is offline Member
    Join Date
    Dec 2011
    Posts
    12
    Rep Power
    0

    Default Re: Method that reads text files and deletes a specific string?

    Ok, I just don't have a clue how I would do that..

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Method that reads text files and deletes a specific string?

    You are already reading a file line by line and, in your original post, writing those lines after a substitution has been made. All I am suggesting is that those files be *different*. Ie, not both "Amulets.txt".

    Once that is done (and verified) you can think about deleting and renaming the files. (Using methods in the File class.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Method that reads text files and deletes a specific string?

    You could build a String first and then write that new String to the file.

    Java Code:
    ...
    String nextLine = "";
    while (...) {
        if (line.indexOf(substring)>0) {
            nextLine += newstring;
        } else {
            nextLine += line;
        }
    }
    ...
    and then after the loop write the whole string to a new file.

Similar Threads

  1. Preferred method for saving/accessing text files
    By joeyvitoro in forum New To Java
    Replies: 5
    Last Post: 12-05-2011, 08:32 PM
  2. How to find specific text from .txt file
    By krechlich in forum New To Java
    Replies: 11
    Last Post: 03-18-2011, 07:57 AM
  3. Button text to specific SQL reference
    By Beck123 in forum NetBeans
    Replies: 1
    Last Post: 01-13-2011, 05:36 PM
  4. Replies: 0
    Last Post: 12-08-2010, 08:40 AM
  5. Replies: 0
    Last Post: 01-27-2010, 04:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •