Somewhere to save....

Hi all,
       I need to write a config file to an easy to find place. i first thought to just use c:\ but then remembered that my other machine doesn't have c: but f:  instead ( go figure ). anyway what i  was wondering was whether or not there is an easy way to find the system drive from Java?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

zzynxConnect With a Mentor Software engineerCommented:
>>I need to write a config file to an easy to find place
Why not use the user's home directory?

zzynxSoftware engineerCommented:
On Windows e.g. that will return

     C:\Documents and Settings\<userid>

The drive is the system drive
petepalmerAuthor Commented:
That will do very nicely thank you :)
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

zzynxSoftware engineerCommented:
You're welcome.
Thanks for accepting
I like to keep my jars, config files and starter batch files in (almost) the same place. This code gives you the jar file path:

     String path;

            String className = getClass(  ).getName(  );
            className = className.substring( className.lastIndexOf( '.' ) + 1 )
                        + ".class";

            path = getClass(  ).getResource( className ).getPath(  );
            path = URLDecoder.decode( path, "UTF-8" );

            int pos = path.substring( 0, path.indexOf( '!' ) ).lastIndexOf( '/' );
            path = path.substring( path.indexOf( '/' ) + 1, pos );
            if( System.getProperty( "file.separator" ).equals( "/" ) )
                //include "/" at start for UNIX OS
                path = "/" + path;
        catch( Exception ex )
            System.out.println( "Could not init application paths; " + ex.getMessage(  ) );
Wow, that was fast...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.