Science & Mathematics Writing to a text file in java

Remove this Banner Ad

power09

Club Legend
Dec 7, 2008
2,872
1,836
Burra SA
AFL Club
Port Adelaide
Other Teams
Timberwolves
Hello Iv made a game in java and I'm trying to save the current state of the game(score, lives remaining etc) and the high scores.

I know ill have to go about this by saving to a text file but the are a lot of ints and strings ill have to save, so is there a way to just input the the names of the variables instead of their actual value in the writing method?
But still have their value written on the text file?

And i guess each value would be written onto its own line so it is easy to parse the values back off the text file.

I was just wondering if someone could write an example method of something like this, because I'm struggling to find an example like this on stack overflow.

would be much appreciated
Cheers
 

Log in to remove this ad.

Hello Iv made a game in java and I'm trying to save the current state of the game(score, lives remaining etc) and the high scores.

I know ill have to go about this by saving to a text file but the are a lot of ints and strings ill have to save, so is there a way to just input the the names of the variables instead of their actual value in the writing method?
But still have their value written on the text file?

And i guess each value would be written onto its own line so it is easy to parse the values back off the text file.

I was just wondering if someone could write an example method of something like this, because I'm struggling to find an example like this on stack overflow.

would be much appreciated
Cheers

There are plenty of good java forums around. Java Ranch is one, have a look through there. There will be this example.
 
Hey guys thanks for trying to help, I worked it out last week, for the records what I was trying to do was this (to write too txt file):

Code:
int toSave = JOptionPane.showConfirmDialog(null, "Do You Want Save the HighScores", s, WIDTH);
                    if(toSave == JOptionPane.YES_OPTION){
                        try {                       
                            BufferedWriter highscores = null;
                            highscores = new BufferedWriter(new FileWriter("C:\\Documents\\NetBeansProjects\\Brickes\\HighScores.txt"));
                            System.out.println("get here?3");
                            highscores.append(firstplayer);
                            highscores.newLine();
                            highscores.append( String.valueOf(HighScore1));
                            highscores.newLine();
                            highscores.append(secondplayer);
                            highscores.newLine();
                            highscores.append(String.valueOf(HighScore2));
                            highscores.newLine();
                            highscores.append(thirdplayer);
                            highscores.newLine();
                            highscores.append(String.valueOf(HighScore3));
                            highscores.newLine();
                            highscores.append(forthplayer);
                            highscores.newLine();
                            highscores.append(String.valueOf(HighScore4));
                            highscores.newLine();
                            highscores.append(fithplayer);
                            highscores.newLine();
                            highscores.append(String.valueOf(HighScore5));
                            highscores.newLine();
                            highscores.close();
                            System.out.println("writhing highscores.....");           
                        } catch (IOException ex) {
                          System.out.println("I/O ERROR!");
                           
                        }
                    }
                    System.exit(0);
 
and this to read from:
Code:
  try{
      scan = new Scanner (new File("C:\\Documents\\NetBeansProjects\\Brickes\\HighScores.txt"));
      System.out.println("Loading HighScores");
      String P1 = scan.nextLine();
      String P1S = scan.nextLine();
      String P2 = scan.nextLine();
      String P2S = scan.nextLine();
      String P3 = scan.nextLine();
      String P3S = scan.nextLine();
      String P4 = scan.nextLine();
      String P4S = scan.nextLine();
      String P5 = scan.nextLine();
      String P5S = scan.nextLine();
      System.out.println(P1 +" : " +P1S);
      System.out.println(P2 +" : " +P2S);
      System.out.println(P3 +" : " +P3S);
      System.out.println(P4 +" : " +P4S);
      System.out.println(P5 +" : " +P5S);
     
      firstplayer= P1;secondplayer = P2; thirdplayer = P3; forthplayer =P4; fithplayer= P5;
      HighScore1 = Integer.parseInt(P1S); HighScore2 = Integer.parseInt(P2S);HighScore3 = Integer.parseInt(P3S);HighScore4 = Integer.parseInt(P4S);HighScore5 = Integer.parseInt(P5S);
     
  }    catch(Exception e){
            System.out.println("could not find file");
  }
 

Remove this Banner Ad

Back
Top