如何在文件中添加非文本?

时间:2022-01-04 03:03:18

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 个解决方案

#1


2  

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

您可以创建可序列化对象并将信息保存在该对象中,并将文件另存为.ser可序列化文件中的对象

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();
    }
}

#2


0  

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

也许你在搜索二进制文件,就像在这里:Java:如何编写二进制文件?

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.

请参阅https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html作为char-to-int转换表。

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

除此之外,您必须在将对象写入文件之前将其转换为字符串(或任何其他类型的串行表示)。

#1


2  

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

您可以创建可序列化对象并将信息保存在该对象中,并将文件另存为.ser可序列化文件中的对象

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();
    }
}

#2


0  

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

也许你在搜索二进制文件,就像在这里:Java:如何编写二进制文件?

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.

请参阅https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html作为char-to-int转换表。

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

除此之外,您必须在将对象写入文件之前将其转换为字符串(或任何其他类型的串行表示)。