Any way to rename a current File without creating a new File object?

C

C-man

Basically I have this little program that will look through directories and
rename any file it finds. The renaming that takes place is like removing of
dashes or Caps the first letter from each word and so on. Is there a better
way to rename the files instead of creating a new file and calling
originalFile.renameTo(new File(newFilename)); Basically I want to know if
creating this new File each time will be costly in both memory and time when
I run the program on hundreds of files. Is there some way to just change the
string of the current file without creating a totally new file?


Thanks Alot
 
R

Roedy Green

s there a better
way to rename the files instead of creating a new file and calling
originalFile.renameTo(new File(newFilename));

Surprisingly "new File" does not create a new file. It just creates an
object with a filename in it. It is only when you hand it to the some
io class and open does a file get created.


see http://mindprod.com/jgloss/file.html
 
A

Alan Meyer

C-man said:
Basically I have this little program that will look through directories and
rename any file it finds. The renaming that takes place is like removing of
dashes or Caps the first letter from each word and so on. Is there a better
way to rename the files instead of creating a new file and calling
originalFile.renameTo(new File(newFilename)); Basically I want to know if
creating this new File each time will be costly in both memory and time when
I run the program on hundreds of files. Is there some way to just change the
string of the current file without creating a totally new file?

Although I am a big fan of Java, I have to say that this kind of
task is easier in a scripting language like Perl or Python, that
doesn't do so much to hide the operating system from you.

Alan
 
J

Jim

Although I am a big fan of Java, I have to say that this kind of
task is easier in a scripting language like Perl or Python, that
doesn't do so much to hide the operating system from you.

Alan
But instead of

originalFile.renameTo(new File(newFilename));

You can write

File newFile = new File(newFilename);
if(!newFile.exists()) {
originalFile.renameTo(newFile));
}

and get a little safety checking in the process.

I haven't noticed that creating a lot of File objects is too
much of a problem as long as you make sure the references
are local and go away when you are done with them.

Jim
 
J

Joona I Palaste

Liz said:
pls don't xpost

We're lucky the OP didn't crosspost, then, aren't we?
Please spell correclty, please don't top-post, and please don't quote
the entire article just to say "pls [sic] don't xpost [sic]".
 
J

Joona I Palaste

We're lucky the OP didn't crosspost, then, aren't we?
Please spell correclty, please don't top-post, and please don't quote
^^
Thus holding an old Usenet tradition... =)
the entire article just to say "pls [sic] don't xpost [sic]".
 
L

Liz

So OP put her quest in two NG,
what is xpost if not this?

Joona I Palaste said:
Liz said:
pls don't xpost

We're lucky the OP didn't crosspost, then, aren't we?
Please spell correclty, please don't top-post, and please don't quote
the entire article just to say "pls [sic] don't xpost [sic]".

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A bicycle cannot stand up by itself because it's two-tyred."
- Sky Text
 
C

Christophe Vanfleteren

Liz said:
Liz said:
pls don't xpost

We're lucky the OP didn't crosspost, then, aren't we?
Please spell correclty, please don't top-post, and please don't quote
the entire article just to say "pls [sic] don't xpost [sic]".

So OP put her quest in two NG,
what is xpost if not this?

Top-posting corrected.

You really aren't a fast learner, are you?

Btw, posting the same question in different newsgroups at different times is
called multi-posting.
 
R

Roedy Green

he renaming that takes place is like removing of
dashes or Caps the first letter from each word and so on. Is there a better
way to rename the files instead of creating a new file and calling
originalFile.renameTo(new File(newFilename));

I answered this earlier. "new File" does NOT create a new file. It
just creates an object with a file name in it. No physical file gets
created till you pas that File to say an OutputStream and call the
open method.

So you need not worry about the overhead of renameTo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top