Last Updated: February 25, 2016
·
710
· jamobox

Java System Property Variables

Often when writing a program a file path is needed where a username variable exists. For example, storing files in the user's Temp directory for temporary files. To achieve this, we can use the following method:

System.getProperty(String key);</code>

The method will return the system property based on the sting key that is passed through with the method. Using the above example, here is how to create a file array of the Temp directory.

File directory = new File("c:\\users\\"+System.getProperty("user.name")+"\\AppData\\Local\\Temp");
File[] files = directory.listFiles();

files </code> will now consist of all the files in the user's Temp directory.