Results 1 to 7 of 7
  1. #1
    doobybug is offline Member
    Join Date
    Mar 2008
    Posts
    17
    Rep Power
    0

    Default f.delete() and f.renameTo() does not work

    Hi I am trying to delete a file and rename another one but cannot work!

    f.exists() is returning true but f.delete() and f.renameTo() are not working. Any idea why? They worked once and now it is not working!

    Java Code:
    input.close();                   
                            out.close(); 
                            fstream.close();
                            
                            File f = new File (flight+".txt");
                            if(f.exists()){
                              boolean ans =  f.delete();
                              System.out.println(ans);
                            }
                            File f2 = new File (flight+"B.txt");
                            if(f2.exists()){
                            f2.renameTo(new File(flight+".txt"));
    The variable flight contains "Flight1" and the files exist for sure! :)

    Thanks a lot

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

    Default

    To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.

    db

  3. #3
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Any error message?
    Does destination file already exist?
    Do you have proper permissions?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  4. #4
    doobybug is offline Member
    Join Date
    Mar 2008
    Posts
    17
    Rep Power
    0

    Default

    yes the file exists in fact f.exists() is returning true. i have file permission rights as well

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

  6. #6
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Have you tested as root/administrator?

    If you are running in Linux, check sticky bit and owner, see [man 2 chmod] (I think) for details.

    Edit:
    And sense you use .txt I assume you are using Windows, I do not know if there is anything like sticky bit there.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  7. #7
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    I tested this example and it works as expected.

    Java Code:
    import java.io.File;
    
    public class FileDelete {
        public static void main( String[] args ) {
            FileDelete m = new FileDelete();
            m.deleteFile();    
        }
        
        public void deleteFile() {
            File f1 = new File( "flight+.txt" );
            File f2 = new File( "flight-.txt" );
            
            if( f1.exists() == true ) {
                System.out.println( "File " + f1.toString() + " exists, deleting." );
                f1.delete();
            }
            
            if( f2.exists() == true ) {
                System.out.println( "File " + f2.toString() + " exists, renaming to flight-_new.txt." );
                f2.renameTo( new File( "flight-_new.txt" ) );
            }
        }
    }
    What OS are you using? Do you have read/write privileges in the folder you are deleting and renaming?
    Last edited by stchman; 08-09-2011 at 10:55 AM.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

Similar Threads

  1. applet call dll work in Win2000 but not work in WinXP
    By manhcuongtin4 in forum Java Applets
    Replies: 1
    Last Post: 07-14-2011, 02:45 PM
  2. help~ delete method cant work
    By reeveliew in forum New To Java
    Replies: 4
    Last Post: 05-07-2010, 03:24 AM
  3. Question about File renameTo() method : Abnormal output
    By suvojit168 in forum New To Java
    Replies: 1
    Last Post: 07-25-2009, 09:22 PM
  4. Replies: 2
    Last Post: 04-20-2009, 09:00 AM
  5. Delete
    By Sarinam in forum New To Java
    Replies: 6
    Last Post: 07-23-2008, 12:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •