Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem in moving file  RSS feed

 
aresh babu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all ,

i am writing the following code to move a file to another directory.

// File (or directory) to be moved
File file = new File("filename");

// Destination directory
File dir = new File("directoryname");

// Move file to new directory
boolean success = file.renameTo(new File(dir, file.getName()));
if (!success) {
// File was not successfully moved
}

By executing this code the file is moving to new directory but it is not deleting from old directory.
But i want to delete that file in the old directory when i was moving to new directory.\
What may be the wrong in my code???/

Please help me

Thanks & Regards
Aresh Babu
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many aspects of the behavior of File#renameTo are inherently platform-dependent. On what platform is your code running? Did you encounter any Exceptions when running your code? Can you create a 'runnable' example class to reconstruct the problem?

Apache Commons IO provides a powerful 'FileUtils' class. I'd recommend trying to use it's 'moveFileToDirectory' method ... at least as comparison to the standard Java File class.
-> http://commons.apache.org/io/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!