Variable Keeps Resetting, I need it to stop, but it is a little bit more complicated.
I am currently working on a project that requires files to be generated from data obtained from the user. The only problem is naming the files. For instance, I need the files to be named File1.txt, File2.txt, File3.txt,etc. When I run this program (it has GUI) and keep it running it will create the files like previously described. However, if I close the program, then run it again, (I assume the variable resets) and it starts back at File1.txt, etc. I need it to, no matter if the program has stopped and started 10000 times, to keep going at that increment. Like I said, I have everything fine except the problem of the variable resetting.
Code:
try{
FileWriter toFile = new FileWriter("File" + filenumber + ".txt");
PrintWriter out = new PrintWriter (toFile);
//out.println statement will go here
out.close();
filenumber++;//increases filenumber by one to change the to-be file's name
} catch (IOException e1) {
e1.printStackTrace();
}
Obviously filenumber is the variable associated with the problem I am having.
Re: Variable Keeps Resetting, I need it to stop, but it is a little bit more complica
If you need to maintain the sequence then you have to store this sequence number in a file. This file should store the last file number that have been created. So when your program stopped the last number will be stored in this file. When you run your program the next time it first read the next sequence value from this file and increment it. This way you can always continue the sequence.
Re: Variable Keeps Resetting, I need it to stop, but it is a little bit more complica
Quote:
Originally Posted by
wsaryada
If you need to maintain the sequence then you have to store this sequence number in a file. This file should store the last file number that have been created. So when your program stopped the last number will be stored in this file. When you run your program the next time it first read the next sequence value from this file and increment it. This way you can always continue the sequence.
Sir, thank you for your speedy reply. I will do that right now, and will come back with an update.
Re: Variable Keeps Resetting, I need it to stop, but it is a little bit more complica
Works flawlessly. Thank you so much for your help.