Results 1 to 8 of 8
  1. #1
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

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

    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++;
    		}
    	}
    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.

    Thanks for any help.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  3. #3
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    Thanks for the response Jos. I've tried to close the FileReader, the FileInputStream and all. None of them are working as of yet.

  4. #4
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    (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 :(

  5. #5
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    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++;
    		}
    	}

  6. #6
    Moncleared is offline Member
    Join Date
    Jan 2009
    Posts
    92
    Rep Power
    0

    Default

    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.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks for sharing a solution to your problem with all. Now we all can avoid this problem in the future.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Moncleared View Post
    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.
    I find that a bit strange (especially the lack of a munmap( ... ) method) and I don't have a simple solution. Does a fc.force(true) on the file channel just before you close it help?

    kind regards,

    Jos

Similar Threads

  1. [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
  2. Help deleting a file
    By 3speed in forum New To Java
    Replies: 4
    Last Post: 11-01-2008, 05:27 AM
  3. Deleting from an object
    By vitaminz in forum New To Java
    Replies: 7
    Last Post: 08-10-2008, 03:56 AM
  4. Deleting a File that is opened
    By ravian in forum Advanced Java
    Replies: 6
    Last Post: 01-30-2008, 02:05 PM
  5. deleting elements
    By nalinda in forum New To Java
    Replies: 2
    Last Post: 12-06-2007, 01:42 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
  •