If the question didn't help, maybe this can:
I made a save()
method that worked fine in saving the variables I wanted saved the first time. It saved the items and everything was good. There's only one problem now. I can only change the values of the saved variables in the file.
Here's my method:
public void save() throws IOException {
// creates old file
File fold=new File(sma.getFilePath());
// should delete file...but doesn't
fold.delete();
File fnew=new File(sma.getFilePath());
// should create a NEW file...doesn't
fnew.createNewFile();
// pseudocode...will add what I want to be saved in the future as a String[]
String lines = "test";
try {
FileWriter f2 = new FileWriter(fnew, false);
f2.write(lines);
f2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
It might just be that I'm not actually deleting the file correctly, but I don't know what I'm doing wrong and somehow this worked for Karthik Balakrishnan in his post.
EDIT:
I added a println that printed Saving
to the console when save()
was called, and I didn't get it. This confused me because I specifically call it after my while(running)
is done in my run() method.
Here's the run() method:
public class Game extends Runnable {
public boolean running = true;
public void save() throws IOException {
// creates old file
File fold=new File(sma.getFilePath());
// should delete file...but doesn't
fold.delete();
File fnew=new File(sma.getFilePath());
// should create a NEW file...doesn't
fnew.createNewFile();
// pseudocode...will add what I want to be saved in the future as a String[]
String lines = "test";
try {
FileWriter f2 = new FileWriter(fnew, false);
f2.write(lines);
f2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run(){
//init();
//try {
//load();
//} catch (FileNotFoundException e1) {
//e1.printStackTrace();
//}
//getPPositions();
int fps = 60;
double timePerTick = 1000000000 / fps;
double delta = 0;
long now;
long lastTime = System.nanoTime();
long timer = 0;
int ticks = 0;
while(running){
// if you want:
running = false;
now = System.nanoTime();
delta += (now - lastTime) / timePerTick;
timer += now - lastTime;
lastTime = now;
if(delta >= 1){
tick();
render();
ticks++;
delta--;
}
if(timer >= 1000000000){
System.out.println("Ticks and Frames: " + ticks);
ticks = 0;
timer = 0;
//canChangeSlide = true;
//canOpenGame = true;
}
}
try {
save();
} catch (IOException e) {
e.printStackTrace();
}
//stop();
}
}
public class Fortnite_Randomizer {
public static void main(String[] args) {
Game game = new Game();
}
}
and this is what I got from the console:
Ticks and Frames: 60
(should be "Saving...Did delete file:
true/false")