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

D

dota

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!!!
 
R

Rhino

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
 
C

Clemens Martin

dota said:
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
 
J

Jon A. Cruz

Rhino said:
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.
 
J

Jon A. Cruz

Clemens said:
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.
 
R

Raymond DeCampo

dota said:
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
 
D

dota

Jon A. Cruz said:
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!!!
 
D

dota

Jon A. Cruz said:
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 !!!
 
A

Andrew Thompson

dota said:
"Jon A. Cruz" ...

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top