Re: Keeping Variable Values
It may be. If you read in the values from a text file. The trouble is that they'd have to have the text file I think somewhere on their computer. Also, if they got rid of it or it wasn't in the same directory as the text file, it couldn't find it. Also, I fear that you'd have to create a text file to start with.
Re: Keeping Variable Values
Thank you, this works. :D
EDIT: Okay, so this now brings up the problem of being able to always have the location of the file the same. So for me it would be C:\\UserName\ThisPartIsProbablyTheSame but how do I get the name given to the username each time? Is that possible?
Re: Keeping Variable Values
You would have to keep track of what OS the user is running (see: Find Operating Systems Information,Java Code to Get Operating System Information)
Depending on their OS, where the standard user data location is different, and how to get this information is also different.
Ex.:
For windows 7 (possibly older versions of Windows too, don't know about this for sure), you can find the user's profile with the environment variable USERPROFILE
Code Java:
System.out.println("The current user's profile is located at " + System.getenv("USERPROFILE"));
Re: Keeping Variable Values
Thanks! I solved the problem using:
Code java:
(System.getenv("USERPROFILE")+"\\Downloads\\txtfile.txt"));
I haven't actually tested this, but I assume it'll work for at least all windows users.
Re: Keeping Variable Values
Load the file relative to the application and not he users hard drive, this will remove any platform specific paths. Simply create a dummy file, then do some string operations to get the true file's path...assuming your application is distributed as a jar:
Code :
File file = new File("temp");//create a file - this will be in the same directory as your jar
String dir = file.getAbsolutePath().substring(0, file.getAbsolutePath().length() - 4 );//remove the temp - this gives you the directory
File myFile = new File(dir + "myfilename.txt" );
//read file, or create a new one if it does not exist
Re: Keeping Variable Values
Ah thanks copeg! So this will make it so that no matter where the text file resides, the program will find it, cool.