Extracting a path from a complete filename?

Discussion in 'Java' started by Gunter Hansen, Nov 18, 2005.

  1. Assume I read (from a text file) into a string a file name which includes a prepended path
    like

    String fn = new String("D:\aaa\bbb\ddd\myfile.txt");

    Is there a function which helps me to extract the path part of it similar to

    System.out.println("Path=" + fn.getPathPart());

    which yields

    Path=D:\aaa\bbb\ddd\

    Gunter
    Gunter Hansen, Nov 18, 2005
    #1
    1. Advertising

  2. Gunter Hansen

    Igor Planinc Guest

    Gunter Hansen wrote:
    > Assume I read (from a text file) into a string a file name which includes a prepended path
    > like
    >
    > String fn = new String("D:\aaa\bbb\ddd\myfile.txt");


    You should escape the backslashes. The new String(...) is also not needed.
    String fn = "D:\\aaa\\bbb\\ddd\\myfile.txt";
    That is not too nice, though. Backslash is a file separator on Windows only.
    Class File has four constants you can use to retrieve separators in a
    platform-independent way:
    static String pathSeparator
    static char pathSeparatorChar
    static String separator
    static char separatorChar

    > Is there a function which helps me to extract the path part of it similar to
    >
    > System.out.println("Path=" + fn.getPathPart());


    File f = new File(fn);
    String path = f.getPath();

    > which yields
    >
    > Path=D:\aaa\bbb\ddd\
    >
    > Gunter
    >
    Igor Planinc, Nov 18, 2005
    #2
    1. Advertising

  3. Gunter Hansen

    Seamus Guest

    Gunter Hansen wrote:
    > Assume I read (from a text file) into a string a file name which includes a prepended path
    > like
    >
    > String fn = new String("D:\aaa\bbb\ddd\myfile.txt");
    >
    > Is there a function which helps me to extract the path part of it similar to
    >
    > System.out.println("Path=" + fn.getPathPart());
    >
    > which yields
    >
    > Path=D:\aaa\bbb\ddd\
    >
    > Gunter


    fn.substring(0, fn.lastIndexOf(java.io.FIle.pathSeparatorChar));
    Seamus, Nov 18, 2005
    #3
  4. Gunter Hansen

    Guest

    File file = new File(fn);
    String parentPath = file.getParent();
    , Nov 18, 2005
    #4
  5. Gunter Hansen schrieb:
    > Assume I read (from a text file) into a string a file name which includes a prepended path
    > like
    >
    > String fn = new String("D:\aaa\bbb\ddd\myfile.txt");

    By the way:
    (1) You have to write "D:\\aaa\\bbb\\ddd\\myfile.txt" because of the
    special meaning of \ in strings.
    (2) You can shorten new String("aaaa") to simply "aaaa".
    >
    > Is there a function which helps me to extract the path part of it similar to
    >
    > System.out.println("Path=" + fn.getPathPart());
    >
    > which yields
    >
    > Path=D:\aaa\bbb\ddd\

    I am not sure if I understood you correctly. It seems you want
    String fn = "D:\\aaa\\bbb\\ddd\\myfile.txt";
    File file = new File(fn);
    System.out.println("Path=" + file.getParent());
    See <http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html>

    --
    "Thomas:Fritsch$ops:de".replace(':','.').replace('$','@')
    Thomas Fritsch, Nov 18, 2005
    #5
  6. Gunter Hansen

    Guest

    how about something like this:

    tmp.substring(0, tmp.lastIndexOf("\\"))
    , Nov 18, 2005
    #6
  7. In article <437e007b$0$21950$-online.net>,
    (Gunter Hansen) wrote:

    > Assume I read (from a text file) into a string a file name which includes a
    > prepended path
    > like
    >
    > String fn = new String("D:\aaa\bbb\ddd\myfile.txt");
    >
    > Is there a function which helps me to extract the path part of it similar to
    >
    > System.out.println("Path=" + fn.getPathPart());
    >
    > which yields
    >
    > Path=D:\aaa\bbb\ddd\
    >
    > Gunter


    See <http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html>.

    = Steve =
    --
    Steve W. Jackson
    Montgomery, Alabama
    Steve W. Jackson, Nov 18, 2005
    #7
  8. > String fn = new String("D:\aaa\bbb\ddd\myfile.txt");
    >
    > Is there a function which helps me to extract the path part of it similar
    > to
    >
    > System.out.println("Path=" + fn.getPathPart());
    >
    > which yields
    >
    > Path=D:\aaa\bbb\ddd\


    File#getPath();

    --
    Andrey Kuznetsov
    http://uio.imagero.com Unified I/O for Java
    http://reader.imagero.com Java image reader
    http://jgui.imagero.com Java GUI components and utilities
    Andrey Kuznetsov, Nov 18, 2005
    #8
  9. Gunter Hansen

    Roedy Green Guest

    On 18 Nov 2005 16:25:31 GMT, (Gunter Hansen)
    wrote, quoted or indirectly quoted someone who said :

    >String fn = new String("D:\aaa\bbb\ddd\myfile.txt");


    oops. \ must be doubled in strings.

    See http://mindprod.com/jgloss/literals.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 18, 2005
    #9
  10. Gunter Hansen

    Roedy Green Guest

    On 18 Nov 2005 16:25:31 GMT, (Gunter Hansen)
    wrote, quoted or indirectly quoted someone who said :

    >
    >Is there a function which helps me to extract the path part of it similar to
    >
    >System.out.println("Path=" + fn.getPathPart());


    see http://mindprod.com/jgloss/file.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Nov 18, 2005
    #10
  11. import java.io.File;
    ....
    File f = new File(fn);
    String directory = f.isDirectory() ? f.getPath() : f.getParent();

    RZR
    Rodrigo Zechin, Nov 18, 2005
    #11
  12. On 2005-11-18, Gunter Hansen penned:
    > Assume I read (from a text file) into a string a file name which
    > includes a prepended path like
    >
    > String fn = new String("D:\aaa\bbb\ddd\myfile.txt");
    >
    > Is there a function which helps me to extract the path part of it
    > similar to
    >
    > System.out.println("Path=" + fn.getPathPart());
    >
    > which yields
    >
    > Path=D:\aaa\bbb\ddd\
    >
    > Gunter
    >


    Take a look at the javadocs for the class File, especially
    getPath().

    --
    monique

    Ask smart questions, get good answers:
    http://www.catb.org/~esr/faqs/smart-questions.html
    Monique Y. Mudama, Nov 18, 2005
    #12
  13. Gunter Hansen

    Guest

    > Is there a function which helps me to extract the path part

    Not that I know of, but it would be very simple to write your own
    though. If you need help writing such a function post back here and
    I'll give you a hand.
    , Nov 20, 2005
    #13
    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. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,004
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,436
    Roland de Ruiter
    Jun 15, 2006
  3. Ed
    Replies:
    10
    Views:
    45,742
    alok000707
    Jul 13, 2010
  4. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    734
    Jonathan N. Little
    May 30, 2008
  5. AAaron123
    Replies:
    6
    Views:
    1,296
    AAaron123
    Oct 13, 2009
Loading...

Share This Page