• Post Reply Bookmark Topic Watch Topic
  • New Topic

File rename using Java API

 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to rename multiple files in one shot using any java api?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!