Results 1 to 19 of 19
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default deleting text file

    I am trying to delete a file. it says that it exists, but it won't delete it. here is my code:

    Java Code:
    public static void editTask(String task, int taskNumber) throws IOException {
    
    		File inFile = new File("tasks.txt");
    		File tempFile = new File(inFile.getAbsolutePath() + ".txt");
    		BufferedReader br = new BufferedReader(new FileReader(inFile));
    		PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
    		String line = null;
    
    		while ((line = br.readLine()) != null) {
    			if (!line.trim().equals("Task3Ødo this!Ø1§")) {
    				pw.println(line);
    				pw.flush();
    			}
    		}
    		pw.close();
    		br.close();
    		
    		// CHECK IF FILE EXISTS FIRST!!!! - PASSED
    		boolean success = inFile.exists();
    		if (success) {
    			System.out.println("file exists");
    		}
    		
    		// TRY DELETING FILE - FAILED!
    		if (!inFile.delete()) {
    			System.out.println("Could not delete file");
    		}
    }
    here is my output:

    Java Code:
    file exists
    Could not delete file

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: deleting text file

    Your code works for me. The file is deleted.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: deleting text file

    Check that you haven't got the file open in some other software which is preventing it being deleted. Your IDE or a text editor or whatever.

  4. #4
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: deleting text file

    I made sure that it wasn't open. it still doesn't seem to delete for me. could it be permissions?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: deleting text file

    Yes, permissions can be a problem. Can you delete the file from the command line or whatever? What about while your program is running, but before editTask() is called?

    If you're on Windows you could reboot and try again. That stoopid OS holds files open for reasons of its own.

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: deleting text file

    tried restarting, then ran the program. still doesn't work.
    and it looks like i can delete it from my main class.

    i'm probably doing something really stupid! haha

    tried calling on a method solely for deleting the file, and it was successful. so it must be something with my method that i call on...

    i think my issue lies in closing a stream/reader somewhere... it won't delete a file that i am working with(tasks.txt) but it will work for a file such as deleteMe.txt...
    Last edited by droidus; 01-19-2012 at 11:23 PM.

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: deleting text file

    issue resolved.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: deleting text file

    Could you explain what the problem was? It Might help the solve the next problem like this.

  9. #9
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: deleting text file

    in some previous methods, i forgot to close the FileInputStream and BufferedReader

  10. #10
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: deleting text file

    here is my code:

    Java Code:
    public static void editTask(String task, int taskNumber) throws IOException {
    
    		File inFile = new File("tasks.txt");
    		File tempFile = new File(inFile.getAbsolutePath() + ".txt");
    		BufferedReader br = new BufferedReader(new FileReader(inFile));
    		PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
    		String line = null;
    
    		// Read from the original file and write to the new
    		// unless content matches data to be removed
    		while ((line = br.readLine()) != null) {
    			if (!line.trim().equals(task)) {
    				// pw.println(line);
    				pw.flush();
    			}
    		}
    		pw.close();
    		br.close();
    		
    		// Attempt to delete the file
    		if (!inFile.delete()) {
    			System.out.println("FATAL ERROR: Could not delete file");
    			System.exit(0);
    		}
    		// Rename the new file to the filename the original file had
    		if (!tempFile.renameTo(inFile)) {
    			System.out.println("FATAL ERROR: Could not rename file");
    			System.exit(0);
    		}
    
    		FileInputStream fs = new FileInputStream("tasks.txt");
    		BufferedReader br2 = new BufferedReader(new InputStreamReader(fs));
    
    		// Write the user-inputed task
    		for (int i = 0; i < taskNumber; ++i)
    			br2.readLine();
    		String lineIWant = br2.readLine();
    
    		// Get the modified version of the text
    		System.out.println("\nModify this existing task: ");
    		// print out the task so the user can modify it
    		System.out.print(task);
    		Scanner input = new Scanner(System.in);
    		String modifiedTask = input.nextLine();
    
    		// Prepare to write to the text file, adding the modified task specified
    		// from the user
    		FileWriter fstream = new FileWriter("tasks.txt", true);
    		BufferedWriter out = new BufferedWriter(fstream);
    
    		// Write the modified task to the .txt file
    		out.write(modifiedTask);
    		out.close();
    		fstream.close();
    		fs.close();
    		br2.close();
    		System.out.println("\nline i want: " + lineIWant);
    	}
    will run once, does fine. when i run again, it says it can't delete the file. close and open eclipse, and it allows me to run it once again, with out any problem. again, run it for a second time, and get the "cannot delete file" error message. and when i try to manually delete the tasks.txt file, it says it's in use by the java se platform binary.
    Last edited by droidus; 01-20-2012 at 12:45 AM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: deleting text file

    Thanks.
    Its hard to get student programmers to close files. Most of the time the program exits with no problem.
    In you case there was a problem.

  12. #12
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: deleting text file

    would you possibly review the code above to see where i went wrong :p thanks

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: deleting text file

    where i went wrong
    Please explain what you mean.

  14. #14
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: deleting text file

    well, it doesn't let me delete the file. i thought i had fixed it.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: deleting text file

    Try making a small program that compiles and executes and shows your problem (a SSCCE) and post it here.
    The code you posted earlier deleted the file on my computer.

  16. #16
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: deleting text file

    so i guess you mean just to try and delete that file? that will be the sole purpose of the small program?

    i created a new method, called deleteFile, and it successfully deleted it. so it has to be something in the method that is throwing it off.

    if it helps at all, in the method, i put the delete call right after the creating of inFile. it was successfully deleted. i then put it after the String line = null; and it failed.
    Last edited by droidus; 01-20-2012 at 04:13 AM.

  17. #17
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: deleting text file

    For creating temp files that you don't want to persist after your program has completed, see the File methods createTempFile(...) and deleteOnExit()

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: deleting text file

    the sole purpose of the small program?
    The purpose is so we can execute it and see the problem and be able to work with the small program to find a solution.

  19. #19
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default Re: deleting text file

    Quote Originally Posted by DarrylBurke View Post
    For creating temp files that you don't want to persist after your program has completed, see the File methods createTempFile(...) and deleteOnExit()

    db
    what i'm actually doing, is copying over all the data, except the line (or task) that the user wants to edit. then when they type in the new text, i add it at the end of the new file. the old file is then deleted, and the file left gets the file name renamed. the issue seems to happen off and on still.

Similar Threads

  1. Deleting '.zip' file...
    By vaibhavspawar in forum New To Java
    Replies: 2
    Last Post: 07-02-2010, 11:13 AM
  2. Deleting multiple lines of text from a file
    By barman in forum New To Java
    Replies: 4
    Last Post: 06-29-2010, 09:12 AM
  3. File Not Deleting
    By Moncleared in forum New To Java
    Replies: 7
    Last Post: 02-21-2010, 08:28 PM
  4. [SOLVED] deleting a line from a text file
    By mokonji in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 01:35 PM
  5. Help deleting a file
    By 3speed in forum New To Java
    Replies: 4
    Last Post: 11-01-2008, 05:27 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
  •