Re: How to get the file name from a String containing the path plus the file name?

Discussion in 'Java' started by Mike Schilling, Mar 4, 2010.

  1. www wrote:
    > www wrote:
    >> Hi,
    >>
    >> I have a String "dirA/dirB/dirC/file.txt". How to use String
    >> manipulation technique to get "file.txt"?
    >>
    >> String fullName = "dirA/dirB/dirC/file.txt";
    >>
    >> int index = fullName.lastIndexOf("\\"); //here is a problem, if I
    >> try lastIndexOf("\"), there is a compile error
    >>
    >> String fileName = fullName.subString(index); //but does not work
    >>
    >> Thank you very much.
    >>
    >> ps: I am working on linux.

    >
    > Sorry. I got it work. It was a typo:
    >
    > int index = fullName.lastIndexOf("\\"); //typo
    >
    > int index = fullName.lastIndexOf("/"); //now it is correct


    Better, becasue it will handle more general file name syntax (e.g. both /
    and \ on Wondows):

    File f = new File(str);
    String name = f.getName();
    Mike Schilling, Mar 4, 2010
    #1
    1. Advertising

  2. Mike Schilling

    Daniel Pitts Guest

    Re: How to get the file name from a String containing the path plusthe file name?

    On 3/4/2010 12:31 PM, Mike Schilling wrote:
    > www wrote:
    >> www wrote:
    >>> Hi,
    >>>
    >>> I have a String "dirA/dirB/dirC/file.txt". How to use String
    >>> manipulation technique to get "file.txt"?
    >>>
    >>> String fullName = "dirA/dirB/dirC/file.txt";
    >>>
    >>> int index = fullName.lastIndexOf("\\"); //here is a problem, if I
    >>> try lastIndexOf("\"), there is a compile error
    >>>
    >>> String fileName = fullName.subString(index); //but does not work
    >>>
    >>> Thank you very much.
    >>>
    >>> ps: I am working on linux.

    >>
    >> Sorry. I got it work. It was a typo:
    >>
    >> int index = fullName.lastIndexOf("\\"); //typo
    >>
    >> int index = fullName.lastIndexOf("/"); //now it is correct

    >
    > Better, becasue it will handle more general file name syntax (e.g. both /
    > and \ on Wondows):
    >
    > File f = new File(str);
    > String name = f.getName();

    I wonder if it works on VMS, where the native structure is strange :)


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
    Daniel Pitts, Mar 4, 2010
    #2
    1. Advertising

  3. Mike Schilling

    Arne Vajhøj Guest

    Re: How to get the file name from a String containing the path plusthe file name?

    On 04-03-2010 17:22, Daniel Pitts wrote:
    > On 3/4/2010 12:31 PM, Mike Schilling wrote:
    >> www wrote:
    >>> www wrote:
    >>>> I have a String "dirA/dirB/dirC/file.txt". How to use String
    >>>> manipulation technique to get "file.txt"?
    >>>>
    >>>> String fullName = "dirA/dirB/dirC/file.txt";
    >>>>
    >>>> int index = fullName.lastIndexOf("\\"); //here is a problem, if I
    >>>> try lastIndexOf("\"), there is a compile error
    >>>>
    >>>> String fileName = fullName.subString(index); //but does not work
    >>>>
    >>>> Thank you very much.
    >>>>
    >>>> ps: I am working on linux.
    >>>
    >>> Sorry. I got it work. It was a typo:
    >>>
    >>> int index = fullName.lastIndexOf("\\"); //typo
    >>>
    >>> int index = fullName.lastIndexOf("/"); //now it is correct

    >>
    >> Better, becasue it will handle more general file name syntax (e.g. both /
    >> and \ on Wondows):
    >>
    >> File f = new File(str);
    >> String name = f.getName();

    > I wonder if it works on VMS, where the native structure is strange :)


    Java on VMS prefer Unix style paths.

    So "dirA/dirB/dirC/file.txt" and not "[.dirA.dirB.dirC]file.txt" ...

    Arne
    Arne Vajhøj, Mar 5, 2010
    #3
    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. The LoxFather

    C plus plus vs C Sharp

    The LoxFather, Aug 9, 2003, in forum: C++
    Replies:
    23
    Views:
    9,159
  2. The LoxFather

    C plus plus vs C Sharp

    The LoxFather, Aug 9, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,349
  3. Replies:
    4
    Views:
    20,278
  4. Rahul
    Replies:
    9
    Views:
    406
    Andrey Tarasevich
    Mar 25, 2008
  5. furqan shaikh

    c plus plus code comparator

    furqan shaikh, Nov 7, 2008, in forum: C++
    Replies:
    6
    Views:
    800
    red floyd
    Nov 12, 2008
Loading...

Share This Page