How to add a pathSeparator to a path (JFileChooser question)  
Author Message
DOTA_4AmP





PostPosted: 2004-2-18 6:47:00 Top

java-programmer, How to add a pathSeparator to a path (JFileChooser question) 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





PostPosted: 2004-2-18 7:07:00 Top

java-programmer >> How to add a pathSeparator to a path (JFileChooser question) 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" <email***@***.com> wrote in message
news:email***@***.com...
> 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!!!


 
Clemens Martin





PostPosted: 2004-2-18 7:27:00 Top

java-programmer >> How to add a pathSeparator to a path (JFileChooser question)
"dota" <email***@***.com> schrieb im Newsbeitrag
news:email***@***.com...
> 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


 
 
Jon A. Cruz





PostPosted: 2004-2-18 12:38:00 Top

java-programmer >> How to add a pathSeparator to a path (JFileChooser question) 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





PostPosted: 2004-2-18 12:48:00 Top

java-programmer >> How to add a pathSeparator to a path (JFileChooser question) Clemens Martin wrote:
> "dota" <email***@***.com> schrieb im Newsbeitrag
> news:email***@***.com...
>
>>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.


 
 
Raymond DeCampo





PostPosted: 2004-2-18 20:13:00 Top

java-programmer >> How to add a pathSeparator to a path (JFileChooser question) 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
 
 
DOTA_4AmP





PostPosted: 2004-2-19 1:04:00 Top

java-programmer >> How to add a pathSeparator to a path (JFileChooser question) "Jon A. Cruz" <email***@***.com> wrote in message news:<email***@***.com>...
> Clemens Martin wrote:
> > "dota" <email***@***.com> schrieb im Newsbeitrag
> > news:email***@***.com...
> >
> >>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_4AmP





PostPosted: 2004-2-19 2:17:00 Top

java-programmer >> How to add a pathSeparator to a path (JFileChooser question) "Jon A. Cruz" <email***@***.com> wrote in message news:<email***@***.com>...
> Clemens Martin wrote:
> > "dota" <email***@***.com> schrieb im Newsbeitrag
> > news:email***@***.com...
> >
> >>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 !!!
 
 
Andrew Thompson





PostPosted: 2004-2-19 2:36:00 Top

java-programmer >> How to add a pathSeparator to a path (JFileChooser question) 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.