Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How to save statistics of a game in a binary file instead of txt file?

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default 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
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Saving statistics in a binary file

    Quote Originally Posted by Freaky Chris View Post
    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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  4. #4
    Junior Member
    Join Date
    Jun 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Saving statistics in a binary file

    Quote Originally Posted by JavaPF View Post
    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

  5. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default 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.

    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:

    File written
    Reading file
    Read file

    Results from file:
    Wins = 10
    Loses = 2
    Ties = 1
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM