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