This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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
    Sheriff
    Posts: 16083
    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 ]
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!