• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File rename using Java API

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to rename multiple files in one shot using any java api?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use Runtime.exec to issue a shell command that does this, but there is no support for this in the Java class libraries.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Presumably you know about File.renameTo(), right? It's just one file at a time, but you can always write a loop. The bigger headache is remembering to check the return value, and if it's false, figuring out why. I hate those methods that return a boolean with no useful diagnostic message if a failure occurs - File has several of these. Jakarta Commons IO has an assortment of methods that fix some of these problems, e.g. FileUtils.forceDelete() and FileUtils.forceMkDir(), but there's apparently nothing like this for forceRename() or forceMove(). Got to do it yourself I guess.
 
reply
    Bookmark Topic Watch Topic
  • New Topic