Results 1 to 5 of 5
  1. #1
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Troubles with deleting files

    Hi guys,

    I wrote a program that removes a line from a CSV file, and it does this by writing everything out to a temp file except for the line I want to remove. Then, it simply deletes the original file and renames the current temp file with the original file name. The problem when I step through with the debugger is here:

    if (!path_to_csv_file.delete())
    {
    System.err.println("ERROR IN removeLineFromFile: COULD NOT DELETE FILE");
    return;
    }

    The if statement is always tripped. Why wouldn't I be able to delete the file? (The file does exists, and I can write to it.) I read the SecurityException that can be thrown by the method delete(), but I haven't overridden any of its methods, nor does the file have special permissions.

    I really don't know what relevant info I could post, so let me know if you'd like additional info. Thanks in advance for any advice!

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Troubles with deleting files

    Can you post more of your code that has to do with opening/reading/closing the file? The file will fail to delete if you still have it open from a stream reader like BufferedReader. You have to .close() those before it will work.

  3. #3
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Re: Troubles with deleting files

    That could be it, Shoss. I made some changes to reflect what you said, but perhaps I'm not seeing the real error.

    temp_file = new File(path_to_csv_file.getAbsolutePath() + ".tmp");
    br = new BufferedReader(new FileReader(path_to_csv_file));
    pw = new PrintWriter(new FileWriter(temp_file));

    finally
    {
    if (pw != null) pw.close();
    try
    {
    if (br != null) br.close();
    // Delete the original file
    if (!path_to_csv_file.delete())
    {
    System.err.println("ERROR IN removeLineFromFile: COULD NOT DELETE FILE");
    return;
    }

    // Rename the new file to the filename the original file had.
    if (!temp_file.renameTo(path_to_csv_file)) System.err.println("ERROR IN removeLineFromFile: COULD NOT RENAME THE FILE");
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    }

    Like I said, when it passes by this piece of code, the console prints "ERROR IN removeLineFromFile: COULD NOT DELETE FILE," so I'm still not able to delete the file for some reason.

  4. #4
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Troubles with deleting files

    Hm, the code looks like it should be fine. I'm running pretty much the same thing you are and its working

    Java Code:
    File path_to_csv_file = new File("C:\\Documents and Settings\\blah\\Desktop\\test.txt");
    File temp_file = new File(path_to_csv_file.getAbsolutePath() + ".tmp");
    
    BufferedReader br = null;
    PrintWriter pw = null;
    
    try
    {
    	br = new BufferedReader(new FileReader(path_to_csv_file));
    	pw = new PrintWriter(new FileWriter(temp_file));
    }
    catch (IOException e)
    {
    	System.out.println(e.toString());
    }
    finally
    {
    	if (pw != null)
    		pw.close();
    	
    	try
    	{
    		if (br != null)
    			br.close();
    		else
    			System.out.println("BR is null");
    
    		// Delete the original file
    		if (!path_to_csv_file.delete())
    		{
    			System.out.println("ERROR IN removeLineFromFile: COULD NOT DELETE FILE");
    			return;
    		}
    	
    		// Rename the new file to the filename the original file had.
    		if (!temp_file.renameTo(path_to_csv_file))
    			System.out.println("ERROR IN removeLineFromFile: COULD NOT RENAME THE FILE");
    	}
    	catch (IOException e)
    	{
    		e.printStackTrace();
    	}
    }
    The only other reasons i could think of that would cause the delete to fail was if the file no longer existed or you don't have permissions to delete a file in that directory.


    Edit: Take a look at this post, it might help you figure out whats happening.
    Last edited by Shoss; 12-15-2011 at 10:29 PM.

  5. #5
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Re: Troubles with deleting files

    Thanks Shoss.

    That is strange; your code is working and I took my code snippet and it works fine as a stand alone app. I'm trying to track which files are opening readers and writers on it, I'm getting permissions/access issues. (I followed the advice from the guys on the link!)

Similar Threads

  1. Deleting Files Using Java!
    By Alerhau in forum New To Java
    Replies: 12
    Last Post: 06-13-2011, 10:49 AM
  2. I have troubles with this code ..!
    By Veton in forum Eclipse
    Replies: 4
    Last Post: 06-05-2011, 06:51 PM
  3. Deleting files after filtering the extensions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-25-2008, 08:00 PM
  4. Deleting an directory/subdirectory/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •