Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to add a pathSeparator to a path (JFileChooser question)

Reply
Thread Tools

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

 
 
dota
Guest
Posts: n/a
 
      02-17-2004
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!!!
 
Reply With Quote
 
 
 
 
Rhino
Guest
Posts: n/a
 
      02-17-2004
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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!!!



 
Reply With Quote
 
 
 
 
Clemens Martin
Guest
Posts: n/a
 
      02-17-2004

"dota" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Jon A. Cruz
Guest
Posts: n/a
 
      02-18-2004
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.

 
Reply With Quote
 
Jon A. Cruz
Guest
Posts: n/a
 
      02-18-2004
Clemens Martin wrote:
> "dota" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:(E-Mail Removed) om...
>
>>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.


 
Reply With Quote
 
Raymond DeCampo
Guest
Posts: n/a
 
      02-18-2004
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
 
Reply With Quote
 
dota
Guest
Posts: n/a
 
      02-18-2004
"Jon A. Cruz" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Clemens Martin wrote:
> > "dota" <(E-Mail Removed)> schrieb im Newsbeitrag
> > news:(E-Mail Removed) om...
> >
> >>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!!!
 
Reply With Quote
 
dota
Guest
Posts: n/a
 
      02-18-2004
"Jon A. Cruz" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Clemens Martin wrote:
> > "dota" <(E-Mail Removed)> schrieb im Newsbeitrag
> > news:(E-Mail Removed) om...
> >
> >>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 !!!
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      02-18-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i add a new path into sys.path? wen Python 10 08-19-2005 12:35 PM
Server.MapPath() croaking with "failed to map path", but path is legal awbacker@gmail.com ASP .Net 0 05-12-2005 10:50 PM
absolute path versus relative path in JSP Matt Java 3 07-08-2004 08:31 PM
Bizzare: Invalid path to Access mdb, path is good Keith-Earl ASP .Net 1 05-03-2004 03:25 PM
Tomcat returning 404 error when using additional path (/servlet/myServlet/extra/path/info) Mupota Muchelemba Java 1 02-04-2004 12:43 AM



Advertisments