• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Moving a file to another directory  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

im having problems with moving a file to another directory. Im using a JFileChooser for the user to pick a file, then i use the renameTo() method to move it to another directory. for some reason, the renameTo() method returns false. here is the code, please tell me whats wrong:



any help would be appreciated.
Thanks.
 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ben,
I used to get the same problems some time back. I discovered two reasons for those unsuccessful file renames. I don't think it will be of any help to you but anyways, here they are:
  • When any Input/Output Streams were left open upon the target file
  • OR, when the file already existed in the target location


  • If I am wrong please correct me.
     
    Java Cowboy
    Posts: 16084
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also you cannot move a file from one disk (or partition) to another using the renameTo method. The to and from directories have to be on the same disk (C:, for example).
     
    Ben Wong
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yup that was the problem. The files were in separate directories.
    The solution i used in the end was to create an InputStream and OutputStream then send the bytes over using the write() method.

    thanks for your help.
    [ April 20, 2006: Message edited by: Ben Wong ]
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!