This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes File Deletion, and problems with overwriting instead of adding to file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File Deletion, and problems with overwriting instead of adding to file." Watch "File Deletion, and problems with overwriting instead of adding to file." New topic

File Deletion, and problems with overwriting instead of adding to file.

Ben Robertson

Joined: Jun 07, 2011
Posts: 3

Hey guys, I was gonna put this in the I/O forum, but because i'm pretty new to Java I figured this might actually just be a rookie mistake i'm making and be better suited here, either way, I would really appreciate some help on this:

I am currently writing an IRC bot, originally just for practicing and learning Java, but I am now adding more and more complex functions to it every day, however this latest idea of mine seems to have led me to a brick wall, I was troubleshooting this on my own for four hours last night and it feels like <- that.

The function I am adding is a way to send offline messages to people via a '.pm username: message' command, when you send the command the bot writes a load of stuff to an external .txt, and in my onJoin() method it searches that .txt file for the person who has joined, and if they have been sent a message, will inform them they have new messages. The person then types '.get' and the message is queried to them.

All of this, so far, works, but with the following issues, that I guess I am too inexperienced to work out:

1) When writing the .pm information to the pmList.txt it will only write on the first line, therefore overwriting the last .pm added

2) The pm information needs to be removed from the pmList.txt after they have recieved it, so they don't recieve it every time they join.

Now, I like to find things out for myself before asking people I don't know, so I searched around, and found the best way to handle the removal of the message details from the list, is to have a temp.txt, and write everything in pmList.txt to temp.txt UNLESS it matches the message which was recieved, then delete pmList.txt and rename temp.txt to pmList.txt, this sounded fair enough to me, and the source examples I found made sense, so I set about doing that. But it didnt work. I tried a few different ways of doing it, and none of that worked either, it wasn't deleting or renaming anything. After that I discovered that it is also overwriting the first line of temp.txt with every line of pmList.txt (manually added for testing) instead of adding them, so temp.txt only ever contained the last line of pmList.txt

The following has had commentlines added so you can follow my (probably flawed) logic flow, and again I would really appreciate help with this, this feature is so close to completion, I just need this to work correctly.

Any comments are appreciated

EDIT: the file.delete() and file.renameTo() methods inside my 'failed to delete' and 'failed to rename' sections are just me being cheeky and saying 'ok if you fail to delete the file, delete the file', but to no avail. Long shot really.
Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

Very important: that ; after the if shouldn't be there. It causes the next block to be executed always; after all, the ; is now the body of the if-statement.
Also, the DataInputStream is unnecessary as the InputStreamReader doesn't use any of its, only those of InputStream.

When you create a new FileWriter like this it will clear the current contents. Use one of the other constructors, with a boolean, to make it append instead.

You are here trying to write to the same file as you're reading from. That's not going to work, period. You'd need to write to a temp file.

You only close the BufferedReader around pmList.txt when there's nothing more to read (well, if that ; is removed). You never close the BufferedReader though. As a result, pmList.txt will always have an open FileInputStream, and therefore the file is always busy. That's why the deletion of this file fails. And because the file still exists when you try to rename to it, the renaming fails as well.

How To Ask Questions How To Answer Questions
Ben Robertson

Joined: Jun 07, 2011
Posts: 3

Rob Spoor wrote:Very important: that ; after the if

jeeeez, I didnt even see that, thanks for pointing that out.

I guess it makes sense that you cant write to something you are reading, and I understand what you have said about the input stream always being busy so it cant delete and therefore rename. My bad.

That's cleared things up a bit, it doesnt work yet, but I'm gonna go study Javadoc reading writing and i/o streams a bit closer, i'm obviously implementing things incorrectly.

Thanks again for your help
Rob Spoor

Joined: Oct 27, 2005
Posts: 19649

You're welcome
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37907
Ben Robertson wrote: . . . i/o streams . . .
. . . in which case you won't want to read about I/O Streams with capital letters
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: File Deletion, and problems with overwriting instead of adding to file.
Similar Threads
Deleting a line from a file
writing into the previous line
Create .txt file
Renaming not happening
a file in your Jar, How do I access it in code?