aspose file tools
The moose likes Beginning Java and the fly likes How to update and delete records in a text file? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to update and delete records in a text file?" Watch "How to update and delete records in a text file?" New topic
Author

How to update and delete records in a text file?

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
Hi,
I had a text file in which contains records line by line with ',' as delimiter as I use stringtokenizer and vector to read the data.
The format in the text file likes: Name, Sex, Age.
I want to add 2 functions:
(1) update the record by name, sex or age;
(2) delete the whole line of record;
Do I need to open a temp text file to do it?
And, what is the algorithm can be suggested?
For both of them, I want to firstly read the total line numbers. Then, the line number + Name, Sex, Age will be displayed on the console window. User can choose which line of record to update or delete.
Or, user can search name in order to do that.
But, what is the backend algorithm to handle it? If I have 10 lines of record, I want to delete 7th line, the 7th line of the text file will be blanked. How can I move 8th, 9th and 10th lines of records up by one line in order to fill the blank line?
Do I need to copy the first 6 lines to a temp text file and copy the last 3 lines of records to the same temp file first? and then copy all the content of that temp file back to the original text file? If so, how can I copy the same format of the original file (with '\n') to the temp file? I need the same data structure likes Name, Sex, Age.
However, when I add records, I need to append the text in the original text file, not override it's current content.
Any advice?
Thanks
Andrew
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
You should probably do what you just described.
I would suggest determining what the field and row delimiters are and then figuring out what the key field(s) is for each row.
You could then create your own algorithm. As for determining the number of lines, look at the LineNumberReader class to see if it helps.
By the way, this sounds like a class project. So, excuse me if I am not specific with any of my suggestions.


Hope This Helps
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to update and delete records in a text file?
 
Similar Threads
Performance Problem?
Database .db file question
Prepending to a file?
How to search a Vector array?
Delete from data..