-1

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")
BLUR Clan
  • 13
  • 9
  • try `Files::delete` to actually get an Exception of why it was not deleted – Eugene Aug 25 '18 at 12:57
  • Print out File.delete(), and look if it returns true or false – Donatic Aug 25 '18 at 12:58
  • 1
    @Donatic - Or, you know, use the debugger built into their IDE. :-) No reason to stumble around in the dark with a `println` torch when you an *turn on the lights* with a debugger. – T.J. Crowder Aug 25 '18 at 13:00
  • Hi! Please update your question with a [mcve] demonstrating the problem that we can copy and paste and run for ourselves to see it happen. – T.J. Crowder Aug 25 '18 at 13:11
  • @T.J.Crowder is that better?? – BLUR Clan Aug 25 '18 at 13:19
  • Just a tip, not about your real problem, but i would buffer your FileReader / Writer by using BufferedReader / Writer https://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html – Donatic Aug 25 '18 at 13:23

1 Answers1

0

I figured it out:

It wasn't being called because it was skipping right past it to call the stop() method because I stopped (closed) the game. I needed to make a manual save in-game, meaning in my KeyManager class, I added another keybind, called it save, and then wrote:

// pseudocode
if(keyManager.save) {
    save(); // surround with try-catch
}
BLUR Clan
  • 13
  • 9