• 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 ...
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Problem in moving file  RSS feed

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
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/
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!