2

In saving a game, I want to add ints, Strings, booleans, etc. because that's everything in my game that I want to save. The only problem is that all I can find is how to add text to files? Nothing in there helps for finding how to add numbers and letters to a non-text file.
Right now, this is my code:

private void saveGame() {
    try {
        //Whatever the file path is.
        File statText = new File("F:/BLAISE RECOV/Java/Finished Games/BasketBall/BasketballGame_saves/Games");
        FileOutputStream is = new FileOutputStream(statText);
    } catch (IOException e) {
        System.err.println("Problem writing to the file statsTest.txt");
    }
}
  • 2
    *Serialization* is one thing to investigate https://stackoverflow.com/questions/447898/what-is-object-serialization – Alex K. Feb 01 '18 at 13:11
  • Combine all these fields in a class and use [Serialization](http://www.oracle.com/technetwork/articles/java/javaserial-1536170.html).. – PM 77-1 Feb 01 '18 at 13:12
  • 1
    do you want to "add non-text to a file" or "add numbers and letters to a non-text file", because there are both in the question? some things you can try are serialization or JSON – Kaddath Feb 01 '18 at 13:13
  • I like @AlexK. response. Saving an object is much easier than writing, then parsing a text file. +1 for alex Oh, and +1 for Parsa's answer. Didn't see it. – older coder Feb 01 '18 at 16:15

2 Answers2

3

you can make a serializable object and save your information in that object and save your file as an object in a .ser serializable file

import java.io.Serializable;

public class Save implements Serializable
{
    private int i ; 
    private String s;
    private boolean b;
    public Save(int i, String s, boolean b)
    {
        this.i = i;
        this.s = s;
        this.b = b;
    }
    public int getI() {
        return i;
    }
    public void setI(int i) {
        this.i = i;
    }
    public String getS() {
        return s;
    }
    public void setS(String s) {
        this.s = s;
    }
    public boolean isB() {
        return b;
    }
    public void setB(boolean b) {
        this.b = b;
    }
}

and you can save the object like this:

public static void main(String[] args) 
{
    try
    {
        File file = new File("C:\\Users\\Parsa\\Desktop\\save.ser");
        FileOutputStream output = new FileOutputStream(file);
        ObjectOutputStream objectOutput = new ObjectOutputStream(output);
        Save save = new Save(10,"aaa",true);
        objectOutput.writeObject(save);
        objectOutput.flush();
        objectOutput.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
parsa
  • 987
  • 2
  • 10
  • 23
  • With your filepath, are you creating a new file when you say `save.ser`? –  Feb 03 '18 at 13:42
  • yes when you use FileOutputStream you are automatically making a new file but the rest is inserting an object into that file. if you need the way to extract it as well tell me and i will send you another answer – parsa Feb 03 '18 at 15:04
0

Maybe you a searching for binary file writing, like in here: Java: How to write binary files?

There you write your data directly to your disk as bytes. Another approach would be to convert your integers to chars like

int integer = 65;    
char number = (char) integer; // outputting this will give you an 'A'
...
int loadedInt = (int) number; // loadedInt is now 65

Refer to https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html as the char-to-int conversion table.

Except from that you must convert your object to a string (or any other kind of serial representation) before writing it to a file.