How to rename an existing file
Asked Answered
Z

2

0

I have an existing file in my HDD and I want rename this file.

Zoara answered 10/5, 2010 at 15:50 Comment(3)
Rename it ... on the command line? in Java? What have you attempted to do? How do you have a handle on the file?Charily
hello, i have an existing file in my HDD and i want rename this file. i have looking for the renameTo function but Do I have to erase my file and recreates?Zoara
Make sure you add more information while posting a question, it saves both of our time.Crypt
R
11

Like this:

new File(path).renameTo(new File(newPath));
Rubefaction answered 10/5, 2010 at 15:52 Comment(0)
F
2

You may look at commons-io FileUtils.moveFile. That method tries File.renameTo, and if that fails, tries to copy&delete the file (e.g. when moving from one drive to another).

If you use File.renameTo directly (which should suffice if you only want to rename a file in the same directory), make sure you evaluate the returned boolean value!

Forfend answered 10/5, 2010 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.