Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: file deletion/renaming not successful, don't think I have any streams open

  1. #1
    Junior Member Bottleskup's Avatar
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default [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:
    	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-
    Last edited by Bottleskup; June 21st, 2011 at 04:15 PM. Reason: adding solved tag


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

  3. The Following User Says Thank You to copeg For This Useful Post:

    Bottleskup (June 21st, 2011)

  4. #3
    Junior Member Bottleskup's Avatar
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Replies: 1
    Last Post: April 7th, 2011, 07:01 AM
  2. Replies: 1
    Last Post: March 24th, 2011, 08:22 PM
  3. Renaming labels & text boxes
    By fride360 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 17th, 2011, 02:11 PM
  4. renaming/deleting file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 15th, 2011, 12:18 AM
  5. Open Text file
    By java_kiddy in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 5th, 2010, 02:52 AM

Tags for this Thread