Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Any way to rename a current File without creating a new File object?

Reply
Thread Tools

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

 
 
C-man
Guest
Posts: n/a
 
      04-05-2004
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



 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-05-2004
On Mon, 05 Apr 2004 01:34:24 GMT, "C-man" <(E-Mail Removed)> wrote
or quoted :

>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

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
 
Alan Meyer
Guest
Posts: n/a
 
      04-05-2004
"C-man" <(E-Mail Removed)> wrote in message
news:AI2cc.10976$J56.10956@edtnps89...
> 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


 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      04-05-2004
pls don't xpost

"C-man" <(E-Mail Removed)> wrote in message
news:AI2cc.10976$J56.10956@edtnps89...
> 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
>
>
>



 
Reply With Quote
 
Jim
Guest
Posts: n/a
 
      04-05-2004
On Mon, 5 Apr 2004 15:55:33 -0400, "Alan Meyer" <(E-Mail Removed)>
wrote:

>"C-man" <(E-Mail Removed)> wrote in message
>news:AI2cc.10976$J56.10956@edtnps89...
>> 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
>

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
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      04-06-2004
Liz <(E-Mail Removed)> scribbled the following:
> 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 ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A bicycle cannot stand up by itself because it's two-tyred."
- Sky Text
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      04-06-2004
Joona I Palaste <(E-Mail Removed)> scribbled the following:
> Liz <(E-Mail Removed)> scribbled the following:
>> 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

^^
Thus holding an old Usenet tradition... =)

> the entire article just to say "pls [sic] don't xpost [sic]".


--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It sure is cool having money and chicks."
- Beavis and Butt-head
 
Reply With Quote
 
Liz
Guest
Posts: n/a
 
      04-06-2004
So OP put her quest in two NG,
what is xpost if not this?

"Joona I Palaste" <(E-Mail Removed)> wrote in message
news:c4u78p$qok$(E-Mail Removed)...
> Liz <(E-Mail Removed)> scribbled the following:
> > 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 ((E-Mail Removed)) ------------- Finland --------\
> \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
> "A bicycle cannot stand up by itself because it's two-tyred."
> - Sky Text



 
Reply With Quote
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      04-06-2004
Liz wrote:

>> Liz <(E-Mail Removed)> scribbled the following:
>> > 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.

--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-11-2004
On Mon, 05 Apr 2004 01:34:24 GMT, "C-man" <(E-Mail Removed)> wrote
or quoted :

>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.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Is there any way to save current job to a file in irb? Kuang Dong Ruby 3 09-16-2006 04:50 PM
PythonWin: any way to delete all objects without exiting and without doing it with "del"? dananrg@yahoo.com Python 5 03-03-2006 05:12 PM
how to use Perl to rename the filenames and directory names under current and subdirectories recursively? lucy Perl Misc 6 09-03-2004 06:27 AM
Is there any way to convert an .img file to an .iso file without having a DVD R/RW? Computer Support 1 10-23-2003 03:40 PM



Advertisments