Search code examples
javabufferedreader

Java BufferedReader reads two lines only


So I have this Void function that is suppose to read integers from a .txt file and it works for the most part except it only reads the first two lines I've tried everything to my power and nothing seems to work

public static void load(Player p) {
       String line = "";
        String token = "";
        String token2 = "";
        String[] token3 = new String[3];
        boolean EndOfFile = false;
        BufferedReader characterfile = null;
        boolean File1 = false;

        try {
            characterfile = new BufferedReader(new FileReader("./cache/Player.txt"));
            File1 = true;
        } catch (FileNotFoundException fileex1) {
        }

        if (File1) {
            // new File ("./characters/"+playerName+".txt");
        } else {
            System.out.println("character file not found.");
        }
        try {
            line = characterfile.readLine();
        } catch (IOException ioexception) {
            System.out.println(": error loading file.");
        }
        while (EndOfFile == false && line != null) {
            line = line.trim();
            int spot = line.indexOf("=");
            if (spot > -1) {
                token = line.substring(0, spot);
                token = token.trim();
                token2 = line.substring(spot + 1);
                token2 = token2.trim();
                token3 = token2.split("\t");
                 if (token.equals("Difficulty")) {
                    Player.setDifficulty(Integer.parseInt(token2));
                }
                        else if (token.equals("Player Health")) {
                                //System.out.println(token2);
                            System.out.println("debug");
                                Player.setHealth(Integer.parseInt(token2));
                            }
                        else  if (token.equals("Player Strength")) {
                                //System.out.println(token2);
                                Player.setStrength(Integer.parseInt(token2));
                            }
                        try {
                            line = characterfile.readLine();
                        } catch (IOException ioexception1) {
                            System.out.println("sent");
                            EndOfFile = true;
                        }
                    }
                    try {
                        characterfile.close();
                    } catch (IOException ioexception) {
                    }
        }
        }

this is my save method which functions as intended.

public void save(Player p) {
    BufferedWriter characterfile = null;
    try {
        characterfile = new BufferedWriter(new FileWriter("./cache/Player.txt"));
        characterfile.write("Player Strength = ");
        characterfile.write(Integer.toString(Player.getStrength()));
        characterfile.newLine();
        characterfile.write("Player Health = ");
        characterfile.write(Integer.toString(Player.getHealth()));
        characterfile.newLine();
        characterfile.write("Difficulty = ");
        characterfile.write(Integer.toString(Player.getDifficulty()));
        characterfile.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

this is what my Player.txt Looks like

Player Strength = 0
Player Health = 0
Difficulty = 0

again as i said Player Strength and Player Health will be read but then the token won't even go to Difficulty or will completely skip it, I am completely out of ideas.


Solution

  • You close the file in the while loop.

    move these lines:

    try {
      characterfile.close();
    } catch (IOException ioexception) {
    }
    

    add a `}' before the lines and remove one after.