Determining the user's home path and the current path in Java


Posted on 2014-01-26 in  java


If you're writing a Java app and need to open or save a file, you may wonder how to determine where to look for or put the file. Three options come to mind.

  1. A temporary file, using the system's temp path.

    File temp = File.createTempFile("temp",".txt");
    
  2. The user's home path, or something relative to that.

    System.out.println(System.getProperty("user.home"));
    
  3. The path from which the app was started. It's worth noting that the current path when the app was started is used, not the location of the class or jar file.

    System.out.println((new java.io.File("file.txt")).getAbsolutePath());
    

Here's a little program to show you what these look like in your configuration. They should all return sensible results on Windows, Mac and Linux.

import java.io.File;
    public class UserHome {
        public static void main (String[] args) throws Exception {
            System.out.println(File.createTempFile("temp",".txt").getAbsolutePath());
            System.out.println(System.getProperty("user.home"));
            System.out.println((new java.io.File("file.txt")).getAbsolutePath());
        }
    }

Leave a Comment:


Blog Search

Some Tags

Programming Is Fun

This is a blog about programming for the sheer joy of it. More...

Contact
github.com/wiseley
twitter.com/mattwiseley