Results 1 to 19 of 19
Thread: deleting text file
- 01-18-2012, 11:50 PM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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:
here is my output: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"); } }
Java Code:file exists Could not delete file
- 01-19-2012, 01:36 AM #2
Re: deleting text file
Your code works for me. The file is deleted.
- 01-19-2012, 01:48 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 01-19-2012, 03:40 AM #4
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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?
- 01-19-2012, 04:10 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 01-19-2012, 10:49 PM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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.
- 01-19-2012, 11:28 PM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: deleting text file
issue resolved.
- 01-19-2012, 11:30 PM #8
Re: deleting text file
Could you explain what the problem was? It Might help the solve the next problem like this.
- 01-20-2012, 12:06 AM #9
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: deleting text file
in some previous methods, i forgot to close the FileInputStream and BufferedReader
- 01-20-2012, 12:16 AM #10
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: deleting text file
here is my code:
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.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); }Last edited by droidus; 01-20-2012 at 12:45 AM.
- 01-20-2012, 12:56 AM #11
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.
- 01-20-2012, 12:57 AM #12
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: deleting text file
would you possibly review the code above to see where i went wrong :p thanks
- 01-20-2012, 01:37 AM #13
Re: deleting text file
Please explain what you mean.where i went wrong
- 01-20-2012, 02:04 AM #14
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: deleting text file
well, it doesn't let me delete the file. i thought i had fixed it.
- 01-20-2012, 02:24 AM #15
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.
- 01-20-2012, 03:33 AM #16
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
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.
- 01-20-2012, 07:21 AM #17
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()
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-20-2012, 01:12 PM #18
Re: deleting text file
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.the sole purpose of the small program?
- 01-21-2012, 01:11 AM #19
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
Re: deleting text file
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
-
Deleting '.zip' file...
By vaibhavspawar in forum New To JavaReplies: 2Last Post: 07-02-2010, 11:13 AM -
Deleting multiple lines of text from a file
By barman in forum New To JavaReplies: 4Last Post: 06-29-2010, 09:12 AM -
File Not Deleting
By Moncleared in forum New To JavaReplies: 7Last Post: 02-21-2010, 08:28 PM -
[SOLVED] deleting a line from a text file
By mokonji in forum New To JavaReplies: 0Last Post: 03-10-2009, 01:35 PM -
Help deleting a file
By 3speed in forum New To JavaReplies: 4Last Post: 11-01-2008, 05:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks