Results 1 to 8 of 8
Thread: File Not Deleting
- 02-21-2010, 06:42 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
File Not Deleting
Hey guys, I've been looking at this for hours and can not determine what or why some of the code is holding a lock on the file. The code's intention is to go through a file and do a regex on the file replacing certain text.
The problem is, I create a temp file and put my results in it. Once it's finished I try to delete the main file, say MainFile.txt, so that I can rename the tmp file to MainFile.txt
Here is the code:
I've tried closing the FileInputStream and the FileChannel and even after closing them the file will still not DELETE. I'm kind of lost as to why the file won't be deleted.Java Code:private void convertXMLFile(File list) throws IOException { Scanner s = null; String current = null; try { s = new Scanner(new BufferedReader(new FileReader(list))); File newOut = new File(list+".tmp"); BufferedWriter output = new BufferedWriter(new FileWriter(newOut)); FileInputStream fis = new FileInputStream(list); FileChannel fc = fis.getChannel(); // Create a read-only CharBuffer on the file ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size()); CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf); // Create matcher on file Pattern pattern = Pattern.compile("<sometext2>"); Pattern pattern2 = Pattern.compile("<sometext>"); Matcher matcher = pattern.matcher(cbuf); Matcher matcher2 = pattern2.matcher(cbuf); // Find all matches if (matcher.find() || matcher2.find()){ // Get the matching string System.out.println("Found: " + list.getName()); String lawl = matcher.replaceAll("\n"); s.useDelimiter("\\n"); while( s.hasNext() ){ current = s.next(); current = current.replaceAll("<sometxt2>", "\n"); current = current.replaceAll("<sometext>"," "); output.append(current+"\n"); } output.close(); s.close(); String tmp = list.getPath(); list.delete(); newOut.renameTo(new File(tmp)); cmp++; } } catch (FileNotFoundException e) { err++; System.out.println("File Not Found"); } catch (IOException e) { err++; } }
Thanks for any help.
- 02-21-2010, 06:49 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,374
- Blog Entries
- 7
- Rep Power
- 17
You have a FileReader as well as a FileInputStream open on the file; one of them locks the file for deleting it and I think one of them is a remnant of previous attempts to code this stuff. Delete one of them and close the other one before you delete the file.
kind regards,
Jos
- 02-21-2010, 06:58 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Thanks for the response Jos. I've tried to close the FileReader, the FileInputStream and all. None of them are working as of yet.
- 02-21-2010, 07:15 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
(The requested operation cannot be performed on a file with a user-mapped section open)
I have no idea how to get this to stop :(
- 02-21-2010, 07:21 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
Here is my updated code and the file will still not delete, cleaned up the code a litlte bit as well.
Java Code:private void convertXMLFile(File list) throws IOException { try { FileInputStream fis = new FileInputStream(list); FileChannel fc = fis.getChannel(); // Create a read-only CharBuffer on the file ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size()); CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf); // Create matcher on file Pattern pattern = Pattern.compile("<something1>"); Pattern pattern2 = Pattern.compile("<something2>"); Matcher matcher = pattern.matcher(cbuf); Matcher matcher2 = pattern2.matcher(cbuf); // Find all matches if (matcher.find() || matcher2.find()){ // Get the matching string String lawl = matcher.replaceAll("\n"); lawl = matcher2.replaceAll(" "); String tmp = list.getPath(); //close FileInputStream fis.close(); fc.close(); list.delete(); File newOut = new File(tmp); BufferedWriter output = new BufferedWriter(new FileWriter(newOut)); // Write the New File... output.append(lawl); output.close(); cmp++; } } catch (FileNotFoundException e) { err++; e.printStackTrace(); System.out.println("File Not Found"); } catch (IOException e) { err++; } }
- 02-21-2010, 07:50 PM #6
Member
- Join Date
- Jan 2009
- Posts
- 92
- Rep Power
- 0
For those who care, the problem turns out to be that when using FileChannel on a windows platform you are mapping the file to memory and since there is no unmap() method available, the only way it gets released is by the garabage collection. Even if you force GC you're still LUCKY to get the resources released.
With that said, the best thing to do is not use a FileChannel.
-
Thanks for sharing a solution to your problem with all. Now we all can avoid this problem in the future.
- 02-21-2010, 08:28 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,374
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
[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 -
Deleting from an object
By vitaminz in forum New To JavaReplies: 7Last Post: 08-10-2008, 03:56 AM -
Deleting a File that is opened
By ravian in forum Advanced JavaReplies: 6Last Post: 01-30-2008, 02:05 PM -
deleting elements
By nalinda in forum New To JavaReplies: 2Last Post: 12-06-2007, 01:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks