Here's how you can create a file on startup, and place it in same directory as the .jar:
private void createFile() throws Exception {
File currentDir = new File(Preferences.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
prefs = new File(currentDir.getParentFile().getAbsoluteFile() + "/hackemos-prefs.txt");
if(!prefs.exists()) {
prefs.createNewFile();
String[] defaults = {
"9", // number of items
"100", // correct limit
"0", // amount to get wrong
"75", // delay, in ms
"456,278", // default mouse position for start button
"653,476", // default mouse position for copying text
"686,615", // default mouse position for the text box
"150,100", // Copy drag range
"0" // mac mode, 1 = enabled
};
write(defaults);
}
}
Here's how you can create a file on startup and place it in the Windows appdata folder (a good place for keeping things), or the Linux / Mac equivalent.
public static void initDirs() {
String osName = Hardware.osName.toLowerCase();
if(osName.contains("win")) {
gameDir = new File((System.getenv("APPDATA") + File.separator + "Hidden" + File.separator));
} else if(osName.contains("mac")) {
gameDir = new File(System.getProperty("user.home") + "/Library/Application Support/Hidden"+File.separator);
} else if(osName.contains("nux")) {
gameDir = new File(System.getProperty("user.home"));
}
if(!gameDir.exists()) gameDir.mkdir();
try {
FileOutputStream fos = new FileOutputStream(gameDir + File.separator + fileName);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(object);
out.close();
fos.close();
return true;
} catch(Exception e) {
e.printStackTrace();
System.err.println("Couldn't save game.");
return false;
}
}
This is just some code from some of my old programs.