?
Solved

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

Posted on 2006-05-21
6
Medium Priority
?
337 Views
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 ...
apple;red;round
apple;green;round
orange;orange;round
banana;yellow;long
orange;orange;big

0
Comment
Question by:Wonder19
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 256 total points
ID: 16731562
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.
0
 
LVL 1

Assisted Solution

by:rongasa
rongasa earned 248 total points
ID: 16731753
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.
0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 248 total points
ID: 16732229
Here are examples:
http://javaalmanac.com/egs/java.io/ReadLinesFromFile.html?l=rel
http://javaalmanac.com/egs/java.io/WriteToFile.html?l=rel
http://javaalmanac.com/egs/java.io/AppendToFile.html?l=rel
http://javaalmanac.com/egs/java.io/UseRandomAccessFile.html?l=rel

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.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 248 total points
ID: 16734200
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

864 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