Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Problem in moving file

 
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/
 
I once met a man from Nantucket. He had a tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!