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.
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.