Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: can someone give a code example of deleting data to a text file?

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default can someone give a code example of deleting data to a text file?

    can someone give a code example of deleting data to a text file in GUI?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,085
    My Mood
    Mellow
    Thanks
    184
    Thanked 653 Times in 640 Posts

    Default Re: can someone give a code example of deleting data to a text file?

    What have you tried? What examples have you found searching the Internet that you don't understand or need help modifying for your use?

    You give us a broken example, and we'll help you fix it.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: can someone give a code example of deleting data to a text file?

    here's my code
    i want to delete a data to a txtfile that i typed to my textfield..pls help me ty..
    word = ("");
    word = inputOutputField.getText().trim();
    try{
    BufferedReader br = new BufferedReader(new FileReader("test.txt"));
    String line = null;

    while ((line = br.readLine()) != null) {
    if (!line.trim().equals(word)) {

    }
    br.close();
    }
    }catch (IOException E) {
    System.out.println("Error" + E);

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,085
    My Mood
    Mellow
    Thanks
    184
    Thanked 653 Times in 640 Posts

    Default Re: can someone give a code example of deleting data to a text file?

    Please post your code in code tags. You can learn how here.

    Comment your code as or before you write it.

    Your code:

    identifies the desired word to delete and stores it in the variable word.
    reads a file, "test.txt", a line at a time
    if the line read is NOT the same as the word to be deleted, do nothing
    otherwise, do nothing

    What you need to do is replace the "do nothings" above with what you'd like to do. For example, if you want to delete the line that contains "word", then don't save that line to a collection (an array?) of lines read. Otherwise, save the line to the collection:
    while ( ( line = br.readLine() ) != null )
    {
        // check for lines NOT equal to the target word
        if ( !line.trim().equals( word ) )
        {
            // when found, store the line to a collection like an array
        }
        else
        {
            // no code required to skip saving the line
        }
    }
    When ready, write the resulting collection of saved lines back to the file from which they came, effectively deleting the words that were not saved to the collection.

Similar Threads

  1. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  2. JAVA code for deleting spaces from Text file
    By Maheshgx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 17th, 2012, 06:26 AM
  3. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  4. Program that reads data from a text file...need help
    By cs91 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 3rd, 2010, 07:57 AM
  5. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 12:40 AM