HELP! How can I search a record in a text file and replace a field in that record?

Posted on 2006-05-21
Last Modified: 2011-09-20
I'm so new to Java, please help.  I want to search for a record in a text file. Then delete that record or replace a field in that record?  What option I have?  

input file "List.txt" content ...

Question by:Wonder19
    LVL 92

    Accepted Solution

    to delete it you'll need to create a new file
    you can replace an entry but only if you replace it with the same number of characters, otherwise you'll need to create a new file.
    LVL 1

    Assisted Solution

    In case the problem is the string parsing think of the above code. StringTokenizer parses the file by using the ";"

    StringTokenizer tokenizer = new StringTokenizer(className, ";");
            while (tokenizer.hasMoreTokens())
                String token = tokenizer.nextToken();

    The token could either be String (as above) or StringBuffer. I would suggest StringBuffer due to the functions it has i.e. append.
    So you could read each word, apple, green, ... and append only the ones you like to the StringBuffer.

    Last with respect to the file as mentioned to the above comment you should better recreate a file. I am not sure whether you would not be able to change the file, i simply do not know that, but i am sure it is the more easy way.
    LVL 12

    Assisted Solution

    Here are examples:

    1) You can read the full file in memory (a String) and then you can apply your change. After this rewrite the file.
    2) You can create a temporary file where you put the file modified (for example: read a line from the original file, apply the changes, save on the newer file). After all modifyes you can rename/copy the new-one on the old-one.

    Hope this could help you.
    Bye, Giant.
    LVL 30

    Assisted Solution


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Suggested Solutions

    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now