Skip to content

Instantly share code, notes, and snippets.

@DrPlantabyte
Last active August 29, 2015 14:06
Show Gist options
  • Save DrPlantabyte/1cb763c26b9e70d90d87 to your computer and use it in GitHub Desktop.
Save DrPlantabyte/1cb763c26b9e70d90d87 to your computer and use it in GitHub Desktop.
Example of how to save program config in an OS appropriate config file. Specifically, this example shows how to remember the location of a JFileChooser after the program exits.
import java.util.*;
import javax.swing.*;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SavedConfigExample {
static final String KEY_DIRECTORY = "filechooser_directory";
static final String APPNAME = "Example";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// load config from last time
Properties config = new Properties();
Path lastTimeConfig = getAppFile(APPNAME,"data.properties");
try{
if(Files.exists(lastTimeConfig)){
config.load(Files.newBufferedReader(lastTimeConfig));
}
}catch (IOException ex){
Logger.getLogger(APPNAME).log(Level.WARNING, "Failed to load config file.", ex);
}
// set file chooser directory to last time's directory
JFileChooser jfc = new JFileChooser();
if(config.containsKey(KEY_DIRECTORY)){
File f = new File(config.getProperty(KEY_DIRECTORY));
if(f.isDirectory()){
jfc.setCurrentDirectory(f);
}
}
// do something
jfc.showOpenDialog(null);
// done, save session
config.setProperty(KEY_DIRECTORY, jfc.getCurrentDirectory().getPath());
try{
Files.createDirectories(lastTimeConfig.getParent());
config.store(Files.newOutputStream(lastTimeConfig), "File comments");
}catch (IOException ex){
Logger.getLogger(APPNAME).log(Level.WARNING, "Failed to update config data", ex);
}
}
public static Path getAppFolder(String appName){
Properties sysProps = System.getProperties();
Path programDir;
System.err.println(sysProps.getProperty("os.name"));
switch (sysProps.getProperty("os.name")) {
case ("Windows Vista"):
case ("Windows 7"):
case ("Windows 8"):
case ("Windows 8.1"):
case ("Windows NT"):
programDir = Paths.get(sysProps.getProperty("user.home"), "AppData", "Roaming", appName);
break;
case ("Windows XP"):
case ("Windows 95"):
case ("Windows 98"):
case ("Windows 2000"):
programDir = Paths.get(sysProps.getProperty("user.home"), "Application Data", appName);
break;
case ("Mac OS X"):
programDir = Paths.get(sysProps.getProperty("user.home"), "Library", "Application Support", appName);
break;
case ("Linux"):
case ("Unix"):
case ("FreeBSD"):
case ("Digital Unix"):
case ("Solaris"):
programDir = Paths.get(sysProps.getProperty("user.home"), "." + appName);
break;
default:
programDir = Paths.get(System.getProperty("user.dir"),"data");
break;
}
return programDir;
}
public static Path getAppFile(String appName, String... filePath){
return Paths.get(getAppFolder(appName).toString(), filePath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment