• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Deciding user dir when in root

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,

if i decide the location of the database i do the following :

System.getProperty("user.dir") + System.getProperty("file.separator") + DBNAME

What to do when one starts the application from the root directory in that case a file separator does not have to be added or am i wrong?
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I decided to just let the User specify where the database file location is (while providing a default), which is then saved to the suncertify.properties file. This avoids cases where you try to automatically determine where the file is and nothing exists there.
 
Ricardo Estafan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but when you provide a default you start with the current directory i guess.

what to do if the current directory is the root?
 
Eric Chang
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, for the default, I provided a hard-coded directory, and if they don't have it, then tough luck...that's why you allow them to set it to something different if they need to.
 
Ricardo Estafan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok in case of the databasfile you are right they can choose another. But in case of the helpfile, i have a html document reciding in i will create a reference using (file.separator etc.) user.dir\docs\userguide.html
if the user.dir = c:\
than my reference will become c:\\docs\userguide.html
and as you can see that won't work

is this a problem you think i have to spend time and energy on or not?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ricardo,

Probably the following constructor of the File class does what you need:



The test code

gives as output:

file = C:\docs\userguide.html
file = user.dir\docs\userguide.html

Hope this helps,

Frans.
 
Ricardo Estafan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frans i'm not quite sure what you mean. Thing is: if i create the help based on the user directory obtained from the System properties. then adding a file separator and finally the path to my document problem is when in windows root c:\

the path to my helpfile will become c:\\docs\userguide.html

This won't work unfortunately how to deal with this problem??
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ricardo,

My point is, you should not concatenate file and path names by yourself, but instead use standard Java classes designed for this job. The File class has been designed by Sun to manage file/directory names, so you'll probably want to use this.

If you manually construct strings containing paths to files, you could end up with a platform specific solution which may work fine at your own Windows computer, but does not work at the examiners Sun Solaris workstation.

Frans.
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ricardo Estafan:
Frans i'm not quite sure what you mean. Thing is: if i create the help based on the user directory obtained from the System properties. then adding a file separator and finally the path to my document problem is when in windows root c:\

the path to my helpfile will become c:\\docs\userguide.html

This won't work unfortunately how to deal with this problem??



Just let the user pick the file with the FileDialog and store the absolute file name in the properties file. If there is no properties file simply start the dialog with the default file name and no directory.

The FileDialog and File classes are designed to figure this stuff out, you shouldn't do it yourself, or as Frans pointed out, it won't work on other platforms.

For the help, your best bet is to include the html files in your jar file and use Class.getResource() to return the URL.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic