How to add a pathSeparator to a path (JFileChooser question)

Discussion in 'Java' started by dota, Feb 17, 2004.

  1. dota

    dota Guest

    Hi,
    I can use JFileChooser, getCurrentDirectory, getPath to get the
    current path name. However, how can I add a path separator to its end?

    Assuming, I got a return of

    c:\temp

    , how to get the path separator "\" and add it to the end of the path

    c:\temp\

    Thanks a lot!!!
     
    dota, Feb 17, 2004
    #1
    1. Advertising

  2. dota

    Rhino Guest

    How about using the concatenation operator ("+") and the constant
    File.pathSeparator?

    For example:

    String path = "C:\temp";
    String pathPlusSeparator = path + File.pathSeparator;

    A bulkier alternative would be using the System property that contains the
    path separator for your property:

    String path = "C:\temp";
    String pathPlusSeparator = path + System.getProperty("path.separator");

    Rhino

    "dota" <> wrote in message
    news:...
    > Hi,
    > I can use JFileChooser, getCurrentDirectory, getPath to get the
    > current path name. However, how can I add a path separator to its end?
    >
    > Assuming, I got a return of
    >
    > c:\temp
    >
    > , how to get the path separator "\" and add it to the end of the path
    >
    > c:\temp\
    >
    > Thanks a lot!!!
     
    Rhino, Feb 17, 2004
    #2
    1. Advertising

  3. "dota" <> schrieb im Newsbeitrag
    news:...
    > Hi,
    > I can use JFileChooser, getCurrentDirectory, getPath to get the
    > current path name. However, how can I add a path separator to its end?
    >
    > Assuming, I got a return of
    >
    > c:\temp
    >
    > , how to get the path separator "\" and add it to the end of the path
    >
    > c:\temp\


    String somePath = ...
    String suffixedPath = somePath + java.io.File.separatorChar

    or

    String suffixedPath = somePath + System.getProperty("file.separator"); //
    java.io.File.separatorChar is internally initialized that way

    Regards,

    Clemens Martin
     
    Clemens Martin, Feb 17, 2004
    #3
  4. dota

    Jon A. Cruz Guest

    Rhino wrote:
    > How about using the concatenation operator ("+") and the constant
    > File.pathSeparator?
    >
    > For example:
    >
    > String path = "C:\temp";
    > String pathPlusSeparator = path + File.pathSeparator;


    Not the best thing to do.

    Use File instead.
     
    Jon A. Cruz, Feb 18, 2004
    #4
  5. dota

    Jon A. Cruz Guest

    Clemens Martin wrote:
    > "dota" <> schrieb im Newsbeitrag
    > news:...
    >
    >>Hi,
    >>I can use JFileChooser, getCurrentDirectory, getPath to get the
    >>current path name. However, how can I add a path separator to its end?
    >>
    >>Assuming, I got a return of
    >>
    >> c:\temp
    >>
    >>, how to get the path separator "\" and add it to the end of the path
    >>
    >> c:\temp\

    >
    >
    > String somePath = ...
    > String suffixedPath = somePath + java.io.File.separatorChar
    >
    > or
    >


    Those are usually not the best way to play with things.


    It's safer to play with File objects instead.

    File baseDir = chooser.getCurrentDirectory();
    ....

    File myTempFile = new File( baseDir, "foobar.txt" );

    Among other things, that should keep safe from accidentally getting
    double separators. On Windows that can lead to subtle tricky problems,
    as it can mutate things to UNC paths.
     
    Jon A. Cruz, Feb 18, 2004
    #5
  6. dota wrote:
    > Hi,
    > I can use JFileChooser, getCurrentDirectory, getPath to get the
    > current path name. However, how can I add a path separator to its end?
    >
    > Assuming, I got a return of
    >
    > c:\temp
    >
    > , how to get the path separator "\" and add it to the end of the path
    >
    > c:\temp\
    >
    > Thanks a lot!!!


    BTW, it appears that you do not want the path separator, but the file
    separator. The file separator separates files within paths (/ on Unix,
    \ on Windows). The path separator separators paths in a list of paths
    (e.g. CLASSPATH or PATH environmental variables) :) on Unix, ; on
    Windows). Both of these may be accessed via static variables in the
    java.io.File class.

    Ray
     
    Raymond DeCampo, Feb 18, 2004
    #6
  7. dota

    dota Guest

    "Jon A. Cruz" <> wrote in message news:<>...
    > Clemens Martin wrote:
    > > "dota" <> schrieb im Newsbeitrag
    > > news:...
    > >
    > >>Hi,
    > >>I can use JFileChooser, getCurrentDirectory, getPath to get the
    > >>current path name. However, how can I add a path separator to its end?
    > >>
    > >>Assuming, I got a return of
    > >>
    > >> c:\temp
    > >>
    > >>, how to get the path separator "\" and add it to the end of the path
    > >>
    > >> c:\temp\

    > >
    > >
    > > String somePath = ...
    > > String suffixedPath = somePath + java.io.File.separatorChar
    > >
    > > or
    > >

    >
    > Those are usually not the best way to play with things.
    >
    >
    > It's safer to play with File objects instead.
    >
    > File baseDir = chooser.getCurrentDirectory();
    > ...
    >
    > File myTempFile = new File( baseDir, "foobar.txt" );
    >
    > Among other things, that should keep safe from accidentally getting
    > double separators. On Windows that can lead to subtle tricky problems,
    > as it can mutate things to UNC paths.


    Thank you all. Unfortunately, I still could not get it work by
    following all suggestions posted above. My java version is 1.3.1,
    which has no class related to either path or file separator. Could you
    please help me further? Thanks!!!
     
    dota, Feb 18, 2004
    #7
  8. dota

    dota Guest

    "Jon A. Cruz" <> wrote in message news:<>...
    > Clemens Martin wrote:
    > > "dota" <> schrieb im Newsbeitrag
    > > news:...
    > >
    > >>Hi,
    > >>I can use JFileChooser, getCurrentDirectory, getPath to get the
    > >>current path name. However, how can I add a path separator to its end?
    > >>
    > >>Assuming, I got a return of
    > >>
    > >> c:\temp
    > >>
    > >>, how to get the path separator "\" and add it to the end of the path
    > >>
    > >> c:\temp\

    > >
    > >
    > > String somePath = ...
    > > String suffixedPath = somePath + java.io.File.separatorChar
    > >
    > > or
    > >

    >
    > Those are usually not the best way to play with things.
    >
    >
    > It's safer to play with File objects instead.
    >
    > File baseDir = chooser.getCurrentDirectory();
    > ...
    >
    > File myTempFile = new File( baseDir, "foobar.txt" );
    >
    > Among other things, that should keep safe from accidentally getting
    > double separators. On Windows that can lead to subtle tricky problems,
    > as it can mutate things to UNC paths.


    Ooh, yes. Both java.io.File.separatorChar and
    java.lang.Systom.getProperty("file.separator") works pretty. I don't
    try Jon's method. Anyway, thank you all again !!!
     
    dota, Feb 18, 2004
    #8
  9. dota wrote:
    > "Jon A. Cruz" ...
    >>> "dota" ...

    ....
    >>>> I can use JFileChooser, getCurrentDirectory, getPath to get the
    >>>> current path name. However, how can I add a path separator to its
    >>>> end?

    .....
    >>>> c:\temp

    (->)
    >>>> c:\temp\


    >> Those are usually not the best way to play with things.
    >> It's safer to play with File objects instead.
    >>
    >> File baseDir = chooser.getCurrentDirectory();
    >> ...
    >>
    >> File myTempFile = new File( baseDir, "foobar.txt" );
    >>
    >> Among other things, that should keep safe from accidentally getting
    >> double separators. On Windows that can lead to subtle tricky
    >> problems, as it can mutate things to UNC paths.

    >
    > Ooh, yes. Both java.io.File.separatorChar and
    > java.lang.Systom.getProperty("file.separator") works pretty. I don't
    > try Jon's method.


    You should. It is the best method.

    I myself used the other methods and
    had constant problems with them, for
    the reasons Jon mentioned. Since I
    use the File class itself - no more
    problems.

    --
    Andrew Thompson
    * http://www.PhySci.org/ Open-source software suite
    * http://www.PhySci.org/codes/ Web & IT Help
    * http://www.1point1C.org/ Science & Technology
     
    Andrew Thompson, Feb 18, 2004
    #9
    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. Roedy Green
    Replies:
    0
    Views:
    725
    Roedy Green
    Jul 9, 2003
  2. alexonjava99
    Replies:
    0
    Views:
    1,743
    alexonjava99
    Jul 26, 2003
  3. Mr. Deckard

    Re: JFileChooser & RMI

    Mr. Deckard, Aug 10, 2003, in forum: Java
    Replies:
    2
    Views:
    1,360
    Stephen Gilbert
    Aug 10, 2003
  4. wen
    Replies:
    10
    Views:
    979
    Peter Hansen
    Aug 19, 2005
  5. Replies:
    5
    Views:
    413
Loading...

Share This Page