• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ben Robertson
Greenhorn
Posts: 3
Eclipse IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Robertson wrote: . . . i/o streams . . .

. . . in which case you won't want to read about I/O Streams with capital letters
 
reply
    Bookmark Topic Watch Topic
  • New Topic