How to save statistics of a game in a binary file instead of txt file?
Hello everyone! Im doing a BlackJack in Java for a proyect. And im done with it except for one thing, i must record the statistics after the game is closed ( wins, loses, ties, etc) in a binary file (filename.bin) and be able to load them when the game is reopened, similar to Solitaire statistics and such. I Have an idea on how to do this for a txt file but not for a bin file, any help please.? Thank you :)
Re: Saving statistics in a binary file
You would just do it exactly the same as far as I'm aware, bin files can contain any data.
Chris
Re: Saving statistics in a binary file
Quote:
Originally Posted by
Freaky Chris
You would just do it exactly the same as far as I'm aware, bin files can contain any data.
Chris
Yes I would of said the same thing too. It will be the same as working with a .txt file.
Re: Saving statistics in a binary file
Quote:
Originally Posted by
JavaPF
Yes I would of said the same thing too. It will be the same as working with a .txt file.
i see, well lets say i want to store the wins, loses and ties of a game, and i have that information stored in 3 different int variables, i want to store that info in a bin file, and then be able to retrieve it and add it to a JLabel to display it to the user, something like "Saved Stats". Can anyone help me briefly on how to do that,
i tried doing what i thought it was going to work but it didnt :( , Well , thanks in advance :)
Re: Saving statistics in a binary file
Hey FretDancer69,
I have written this example for you. It will write the scores to a file and then read the file and print the scores.
Code :
import java.io.*;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class FretDancer69 {
/**
* JavaProgrammingForums.com
*/
public static File file = new File("file.bin");
public static int win, lose, tie;
public static int winFile, loseFile, tieFile;
public static void writeFile() {
String newLine = System.getProperty("line.separator");
try {
Writer output = null;
output = new BufferedWriter(new FileWriter(file));
output.write(Integer.toString(win));
output.write(newLine);
output.write(Integer.toString(lose));
output.write(newLine);
output.write(Integer.toString(tie));
output.close();
System.out.println("File written");
readFile();
} catch (Exception e) {
System.out.println("Error writing file");
e.printStackTrace();
}
}
public static void readFile() {
System.out.println("Reading file");
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
winFile = sc.nextInt();
loseFile = sc.nextInt();
tieFile = sc.nextInt();
}
sc.close();
System.out.println("Read file");
} catch (FileNotFoundException e) {
System.out.println(file + " cannot be found!");
}
}
public static void main(String[] args) throws Exception {
win = 10;
lose = 2;
tie = 1;
FretDancer69 fd = new FretDancer69();
fd.writeFile();
System.out.println();
System.out.println("Results from file: ");
System.out.println("Wins = " + winFile);
System.out.println("Loses = " + loseFile);
System.out.println("Ties = " + tieFile);
}
}
Example output:
Quote:
File written
Reading file
Read file
Results from file:
Wins = 10
Loses = 2
Ties = 1