[SOLVED]file deletion/renaming not successful, don't think I have any streams open
Hi, I am pretty new to Java, so theres a good chance this is just a rookie error i've made, however, I cant seem to find the answer anywhere, so I am turning to the wider community.
I am writing an IRC bot currently, and one of the features i decided to add was a way to send messages to people who arent online, the messages are then delivered whenever they next join a channel the bot is in. When someone sends the pm command with the correct args, it is written to a file with a load of other info for later use, when someone joins a channel with the bot in, they are processed through this file, and if they have a message, they are informed of this, and told to type '.get' to recieve the messages.
The person then types '.get', and this is where things start to go wrong. When '.get' is read by the bot, my getHandler() method is called (code below), the txt file with all the PMs in it is written to an Array, and then the array is cycled through, looking for the sender, messages intended for the sender are formatted and delivered, all messages which are not intended for the sender are written to a temp file. All of that is working as intended, however the next step is to delete the original file, and rename the temporary one to the originals name, in order to prevent repeated PMs. I can't seem to make it delete (and therefore rename) the files though, as far as I can see I dont have any I/O streams open to prevent it, but I must have, otherwise it would just work, surely...
Anyway, any help or comment is appreciated, below is the code with some handy commentlines:
Code Java:
public static void getHandler(PircBot bot, String sender, File file) throws IOException{
File tempFile=new File("pmListx.txt");
try{
Scanner in=new Scanner(new FileReader(file));
int i;
while(in.hasNextLine()){
String list0=in.nextLine();
/*
* '#' is used as delimiter for seperating PMs (all written on one line)
*/
String[] allPMs=list0.split("#");
in.close();
for(i=0;i<=allPMs.length;i++){
if(allPMs[i].split(":")[0].contains(sender)){
/*
* if the part of the line where targets are stored contains the sender, it is split up, formatted and sent to them
*/
String toProcess=allPMs[i];
String[] splitCmd=toProcess.split(":");
String[] splitMsg=splitCmd[1].split("~");
String target=splitCmd[0].split(" ")[1].trim();
String msg=splitMsg[0].trim();
String source=splitMsg[1].trim();
bot.sendMessage(target, Colors.BLUE+"***[TTW MAIL]*** "
+Colors.BLACK+"Message from "+source.toUpperCase()+": "+msg);
}
else{
/*
* all lines which are not intended for the sender are written to the tempFile (this also works)
*/
BufferedWriter out=new BufferedWriter(new FileWriter(tempFile, true));
out.append(allPMs[i]);
out.close();
}
}
}
}
finally{
/*
* if file is not deleted, it tells me it isnt
*
* im getting 'File not deleted' every time, is there a stream open?
*/
boolean success=file.delete();
if(!success){
bot.sendNotice("Bottleskup", "File not deleted");
}
else{
/*
* if it is deleted, the tempfile is renamed
*/
tempFile.renameTo(file);
}
}
}
So, really the question is, what am i doing wrong? and what should I do to fix that?
Thank you for your time.
-Ben-
Re: file deletion/renaming not successful, don't think I have any streams open
First, you should not be calling close on the Scanner within a loop which reads from said Scanner. Its best practice to call close within a finally block, so all exceptions that may occur can be handled appropriately. Second, you should handle all exceptions within a catch block, rather than dropping down to the finally without catching any exceptions. Finally, you should make sure the file exists, and your application has the permission to delete the parameter file.
Re: file deletion/renaming not successful, don't think I have any streams open
Ah, thank you for your advice about putting close() in the finally block, that does make a lot more sense in case exceptions are thrown and it doesnt get closed, I stuck a file.exists() check in too, but i finally found out what the problem is when i tried to manually delete the file, and was informed it cannot be deleted because it is open in Java Runtime SE, on closer inspection of a few different methods I have which access the same file, i discovered an in.close() which was not in a finally block, and the conditions were not always met, as such it was often open.
This is now resolved and it all works fine :)
Thank you copeg, for mentioning the close() in a finally block thing, turns out that was a much deeper rooted problem than I thought, I have truly learnt from this mistake :)