Results 1 to 10 of 10
  1. #1
    urbanleg is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default cannot delete file.

    Hi,
    here is a method i use in order to extract a string from a text file named "filename":
    Java Code:
    import java.awt.AWTException;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream.GetField;
    import java.nio.MappedByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.charset.Charset;
    public class example {
    	
    	
    	
    	public static void main(String[] args) throws InterruptedException, IOException, AWTException, LexicalError{
    	
    		String s = fileToString("c:\\1.txt");
    		File f = new File("c:\\1.txt");
    		f.delete();   // DOESNT WORK
    		
    	}
    
    	  static String fileToString(String path) throws IOException {
    			FileInputStream stream = new FileInputStream(new File(path));
    				try {
    						FileChannel fc = stream.getChannel();
    						MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    							/* Instead of using default, pass in a decoder. */
    						
    						fc.close();
    						return Charset.defaultCharset().decode(bb).toString();
    					}
    					finally {
    							
    						stream.close();
    						}
    		}
    	
    	
    	
    }



    The problem is : i cannot delete the file "filename" after this method is called.
    anyone knows why? and how can i delete it anyway?

    Thanks.
    Last edited by urbanleg; 08-30-2011 at 06:36 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Can you make a small complete program that compiles, executes and shows the problem?

  3. #3
    urbanleg is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Java Code:
    public class example {
    	
    	
    	
    	public static void main(String[] args) throws InterruptedException, IOException, AWTException, LexicalError{
    	
    		String s = fileToString("c:\\1.txt");
    		File f = new File("c:\\1.txt");
    		f.delete();   // DOESNT WORK
    		
    	}
    
    	  static String fileToString(String path) throws IOException {
    			FileInputStream stream = new FileInputStream(new File(path));
    				try {
    						FileChannel fc = stream.getChannel();
    						MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    							/* Instead of using default, pass in a decoder. */
    						
    						fc.close();
    						return Charset.defaultCharset().decode(bb).toString();
    					}
    					finally {
    							
    						stream.close();
    						}
    		}
    	
    	
    	
    }

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    You left off the import statement needed to compile the code.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Also, learn how to format your code consistently: Code Conventions for the Java(TM) Programming Language: Contents

    db

  6. #6
    urbanleg is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You left off the import statement needed to compile the code.
    added imports on the main post

  7. #7
    urbanleg is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    ok managed to handle it.
    thanks anyway

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    What did you have to do to fix it?

  9. #9
    urbanleg is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default

    changed the whole method.
    i created a new method which uses SCANNER instead

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

Similar Threads

  1. Replies: 2
    Last Post: 05-26-2011, 06:38 AM
  2. Can't Delete A File
    By blazecode in forum Threads and Synchronization
    Replies: 4
    Last Post: 03-07-2011, 11:16 AM
  3. Delete A File
    By nitinverma in forum CLDC and MIDP
    Replies: 13
    Last Post: 06-15-2010, 09:12 AM
  4. Delete From .txt file
    By Sarinam in forum New To Java
    Replies: 86
    Last Post: 06-28-2008, 11:17 AM
  5. How to delete a file
    By Alpha in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 09:11 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
  •