renameTo() works properly on Unix but not in Windows


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: renameTo() works properly on Unix but not in Windows

  1. #1
    Patrick Guest

    renameTo() works properly on Unix but not in Windows


    I'm trying to simply move (File.renameTo()) a file from a directory to another
    and it works well on Unix but not in Windows.
    I've read other bug requests related to that tried to follow all recomendations
    but nothing works !
    I'm reading an existing file using a BufferedReader and closing it before
    the
    move operation (as I'm closing it on a "finally" statement I'm really sure
    that
    the close operation was done !). But the answer is allways "false" when I
    run
    the application on Windows.
    I also tried to delete the file (file.delete()) and it ocurrs the same way.
    Folowing, the code:

    //Move file to "Done" directory
    File inFile = new File("c:\\Example\\example.txt"); //existing
    File
    File outFile = ("c:\\Example\\Done\\example.txt"); //non existing
    File


    useFile(); //Function that reads the file
    //and closes the BufferedReader using
    // in the "finally" statement

    if (inFile.renameTo(out))
    System.out.println("Move operation successfull !");
    else
    System.out.println("Move operation failed !");


  2. #2
    sachin Guest

    Re: renameTo() works properly on Unix but not in Windows


    hi patrick,
    the file seperator in the unix & windows is different. i.e.,
    d:\\abc in windows
    d://abc in unix.
    to overcome this dependancy use java.io.File.separator
    regards...

    "Patrick" <ptckweiss@freesurf.ch> wrote:
    >
    >I'm trying to simply move (File.renameTo()) a file from a directory to another
    >and it works well on Unix but not in Windows.
    >I've read other bug requests related to that tried to follow all recomendations
    >but nothing works !
    >I'm reading an existing file using a BufferedReader and closing it before
    >the
    >move operation (as I'm closing it on a "finally" statement I'm really sure
    >that
    >the close operation was done !). But the answer is allways "false" when

    I
    >run
    >the application on Windows.
    >I also tried to delete the file (file.delete()) and it ocurrs the same way.
    >Folowing, the code:
    >
    > //Move file to "Done" directory
    > File inFile = new File("c:\\Example\\example.txt"); //existing
    >File
    > File outFile = ("c:\\Example\\Done\\example.txt"); //non existing
    >File
    >
    >
    > useFile(); //Function that reads the file
    > //and closes the BufferedReader using
    > // in the "finally" statement
    >
    > if (inFile.renameTo(out))
    > System.out.println("Move operation successfull !");
    > else
    > System.out.println("Move operation failed !");
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center