File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes need simple example of updating a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need simple example of updating a text file" Watch "need simple example of updating a text file" New topic

need simple example of updating a text file

Robin Clark
Ranch Hand

Joined: Dec 17, 2003
Posts: 81
I am reading an input file and updating one line. The way that I am trying is not working. I can read the input file and create the output file correctly, but I can't rename the output file to the input file's name. Here is my code:

When I try to rename the file, it returns a false value. I think there must be a much simpler way to accomplish this task. Can someone help me?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Well, there are two questions here. First, the easy one: why can't you rename the new file? Because the old one of that name is still there. Delete the old one first (or rename the old, rename the new, then delete the old, to be extra safe) and this should work fine.
Other question: a much simpler way. Basically what you're doing is using users.txt as a flat-file database. The simpler way would be, first, to abstract the whole "flat file database" concept out behind a "UserDatabase" interface with methods like "addUser", "updatePhoneNumber," etc. Then implement this in terms of your flat file, and call it from your servlet.
When everything's working, then you probably want to think about using a real relational database -- at least if the "users" list is more than a few dozen names.

[Jess in Action][AskingGoodQuestions]
Robin Clark
Ranch Hand

Joined: Dec 17, 2003
Posts: 81
Yes! Deleting the file did the trick. After I get this down and dirty prototype working, I am going to clean up the code as you suggest. Right now it is a mess because I am rushing through things that I don't understand properly.
I really appreciate the help.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Something to think about ... how safe do you want this operation to be? Years ago IBM's CMS operating system did something like this when rewriting any existing file:
* write the new file to a temp name, close it
* rename original file to a backup name
* rename the temp file to the original name
* erase the backup file
Think about what is left if you have some kind of failure during any one of these steps. It seems like a lot of work but if your data is critical it may be worth it!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
It is sorta covered in the JavaRanch Style Guide.
subject: need simple example of updating a text file
It's not a secret anymore!