(easy) splitting a file name into parts

Discussion in 'Java' started by Andy Fish, Aug 22, 2003.

  1. Andy Fish

    Andy Fish Guest

    Hi,

    I want to rename a file to have a different extension, but I can't find
    anything in java.io.File that has any concept of file extension.

    I realise this is probably operating system specific if not
    application-specific but since the two biggest OSs use file extensions
    extensively(!), I was expecting java.io to provide some utility functions to
    manipulate them. FWIW I define the file extension as the bit following the
    final dot in the file name but I'm happy to accept someone else's
    definition.

    Andy
     
    Andy Fish, Aug 22, 2003
    #1
    1. Advertising

  2. Andy Fish

    Chris Smith Guest

    Andy Fish wrote:
    > I want to rename a file to have a different extension, but I can't find
    > anything in java.io.File that has any concept of file extension.
    >
    > I realise this is probably operating system specific if not
    > application-specific but since the two biggest OSs use file extensions
    > extensively(!), I was expecting java.io to provide some utility functions to
    > manipulate them. FWIW I define the file extension as the bit following the
    > final dot in the file name but I'm happy to accept someone else's
    > definition.


    Well, that last bit seems like a reasonable definition to me. In that
    case, see String.lastIndexOf and String.substring for easy ways to
    manipulate the extension on a file.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 22, 2003
    #2
    1. Advertising

  3. Andy Fish

    Roedy Green Guest

    On Fri, 22 Aug 2003 11:34:25 GMT, "Andy Fish"
    <> wrote or quoted :

    >I want to rename a file to have a different extension, but I can't find
    >anything in java.io.File that has any concept of file extension.


    see http://mindprod.coml/jgloss/file.html for sample code.

    basically search for last .

    Properly should stop on last / \ also since some people put dots in
    directory names.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 22, 2003
    #3
  4. Andy Fish

    Roedy Green Guest

    On Sat, 23 Aug 2003 11:34:20 -0700, "Jon A. Cruz" <>
    wrote or quoted :

    >Nawww
    >
    >java.io.File.isFile()


    If you are parsing the entire unqualified name yourself, and you scan
    backwards for . you don't want it to count if it occurs prior to the
    last "/", i.e. in one of the directory node names.

    You want to scan only the filename proper, e.g. new File ( f ).
    getName();

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Aug 23, 2003
    #4
  5. Andy Fish

    Jon A. Cruz Guest

    Roedy Green wrote:
    > On Sat, 23 Aug 2003 11:34:20 -0700, "Jon A. Cruz" <>
    > wrote or quoted :
    >
    >
    >>Nawww
    >>
    >>java.io.File.isFile()

    >
    >
    > If you are parsing the entire unqualified name yourself, and you scan
    > backwards for . you don't want it to count if it occurs prior to the
    > last "/", i.e. in one of the directory node names.
    >
    > You want to scan only the filename proper, e.g. new File ( f ).
    > getName();
    >


    Exactly.

    I had assumed that, since it's been covered many times.

    File thing = new File( f );
    if ( thing.isFile() )
    {
    int pos = thing.getName().lastIndexOf( '.' );
    if ( pos > -1 )
    {
    ...
    }
    }

    of course, if pos == 0, you now have the tricky problem of deciding if
    it's a blank name with only an extension, or if it's a hidden unix-ish
    file with no extension.
     
    Jon A. Cruz, Aug 24, 2003
    #5
    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. John Ericson
    Replies:
    0
    Views:
    431
    John Ericson
    Jul 19, 2003
  2. Mark
    Replies:
    0
    Views:
    447
  3. Fabian Steiner

    Splitting device addresses into parts

    Fabian Steiner, Sep 26, 2006, in forum: Python
    Replies:
    9
    Views:
    269
    Lawrence D'Oliveiro
    Sep 27, 2006
  4. AG
    Replies:
    5
    Views:
    443
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Jan 18, 2007
  5. Tore Aursand

    Splitting an array into "even" parts

    Tore Aursand, Nov 5, 2003, in forum: Perl Misc
    Replies:
    5
    Views:
    194
    Tore Aursand
    Nov 5, 2003
Loading...

Share This Page