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

Discussion in 'Java' started by C-man, Apr 5, 2004.

  1. C-man

    C-man Guest

    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
     
    C-man, Apr 5, 2004
    #1
    1. Advertising

  2. C-man

    Roedy Green Guest

    On Mon, 05 Apr 2004 01:34:24 GMT, "C-man" <> 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.
     
    Roedy Green, Apr 5, 2004
    #2
    1. Advertising

  3. C-man

    Alan Meyer Guest

    "C-man" <> 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
     
    Alan Meyer, Apr 5, 2004
    #3
  4. C-man

    Liz Guest

    pls don't xpost

    "C-man" <> 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
    >
    >
    >
     
    Liz, Apr 5, 2004
    #4
  5. C-man

    Jim Guest

    On Mon, 5 Apr 2004 15:55:33 -0400, "Alan Meyer" <>
    wrote:

    >"C-man" <> 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
     
    Jim, Apr 6, 2004
    #5
  6. Liz <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "A bicycle cannot stand up by itself because it's two-tyred."
    - Sky Text
     
    Joona I Palaste, Apr 6, 2004
    #6
  7. Joona I Palaste <> scribbled the following:
    > Liz <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "It sure is cool having money and chicks."
    - Beavis and Butt-head
     
    Joona I Palaste, Apr 6, 2004
    #7
  8. C-man

    Liz Guest

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

    "Joona I Palaste" <> wrote in message
    news:c4u78p$qok$...
    > Liz <> 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 () ------------- Finland --------\
    > \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    > "A bicycle cannot stand up by itself because it's two-tyred."
    > - Sky Text
     
    Liz, Apr 6, 2004
    #8
  9. Liz wrote:

    >> Liz <> 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
     
    Christophe Vanfleteren, Apr 6, 2004
    #9
  10. C-man

    Roedy Green Guest

    On Mon, 05 Apr 2004 01:34:24 GMT, "C-man" <> 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.
     
    Roedy Green, Apr 11, 2004
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jesper Sahner
    Replies:
    4
    Views:
    514
    Boudewijn Dijkstra
    Jan 18, 2005
  2. ajay
    Replies:
    27
    Views:
    753
    Bill Seurer
    Apr 14, 2004
  3. Replies:
    5
    Views:
    441
    Steven Bethard
    Mar 3, 2006
  4. Kuang Dong
    Replies:
    3
    Views:
    105
    Gene Tani
    Sep 16, 2006
  5. lucy
    Replies:
    6
    Views:
    175
    Michele Dondi
    Sep 3, 2004
Loading...

Share This Page