如何保存变量以便以后访问?

时间:2014-03-22 18:53:48

标签: java

我正在尝试制作一个基于文本的游戏,其中你有不同层次的生活和其他变量的不同方面。我正在尝试将它们保存到某种文件中,以便稍后再将它们取回。

到目前为止,这就是我对变量的看法:

public class baseLevels {
    public boolean gameOver = false;

    //100 is a normal level
    public int happyLevel = 100;
    public int sleepLevel = 100;
    public int angerLevel = 100;
    public int healthLevel = 100;
    public int popularityLevel = 100;
    public int smartLevel = 100;
    public int weight = 100;

    //0 = Single 1 = Not very close, but dating 2 = close and dating 3 = engaged 4 = married
    public int relationshipStatus = 0; 
    public boolean children = false;
    public int numberOfChildren = 0;

    //Tests if the player is injured, 1-5 on severeness
    public boolean injured = false;
    public int severity = 1;
    public boolean healthCare = false;

    //Money status: 1 is poor, 5 is rich
    public int money = 5000;
    public int wealthStatus = 1;  

    //Family is alive or dead
    public boolean momDead = false;
    public boolean dadDead = false;
    public boolean sisterDead = false;
    public boolean brotherDead = false;
    public boolean grandmaDead = false;
    public boolean grandpaDead = false;

    //Misc. variables
    public boolean car = false;
    public boolean house = false;
    public boolean dead = false;
    public boolean likeMusic = false;


}

在查看某人发布的属性方法后,我喜欢它,但我需要一种方法将属性值设置为数字。当我尝试这个时,我得到一个错误,说我不能有字符串和整数。

3 个答案:

答案 0 :(得分:4)

对于像这样简单的事情,

java.util.Properties可能是最好的类。它具有内置方法,用于从文件加载并保存到文件。

Properties props = new Properties();
props.load(new FileInputStream("data.props"));
String someProp = props.getProperty("myName");
props.setProperty("myName", "John Smith");
props.store(new PrintWriter("data.props"));

答案 1 :(得分:1)

一种选择是使用对象序列化。

要使用序列化,您的类必须实现Serializable接口。

假设您有一个要写入文件的类baseLevels的实例,“save.dat”:

baseLevels obj = new baseLevels();

使用对象序列化,首先要为保存文件创建一个对象输出流:“save.dat”。

ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream( "save.dat" ) );

将baseLevels实例obj写入要调用writeObject(obj)的文件:

out.writeObject( obj );

永远记得关闭流:

out.close();

要将对象恢复到将其写入保存文件时所处的状态,您将使用对象输入流:

ObjectInputStream in = new ObjectInputStream( new FileInputStream( "save.dat" ) );

并调用方法readObject(),该方法从输入流中读取下一个对象。 readObject()的返回类型是Object,因此我们需要将其强制转换为适当的类型;在这个例子中,我们只写了一个对象,我们知道它是baseLevels类型,因此我们可以安全地将返回的对象强制转换为baseLevels:

baseLevels obj = (baseLevels)in.readObject().

关闭流:

in.close();

完整示例(假设baseLevels实现Serializable):

// Initialize
String      fileName    = "save.dat";
baseLevels  lvl1        = new baseLevels();

// Set level attributes
lvl1.angerLevel = 0;
lvl1.happyLevel = 200;

// Write level
try( ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream( fileName ) ) ) {
    out.writeObject( lvl1 );
}
catch( IOException e ) {
    System.err.println( e.getMessage() );           
}

// Read level
baseLevels lvl2;

try( ObjectInputStream in = new ObjectInputStream( new FileInputStream( fileName ) ) ) {
    lvl2 = (baseLevels)in.readObject();     
}
catch( IOException e ) {
    System.err.println( e.getMessage() );           
} 
catch( ClassNotFoundException e )  {
    System.err.println( "Class definition could not be found" );
    System.err.println( e.getMessage() );
}

有关序列化的更多信息,请查看:http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html

从上述网站链接的示例 - 因为如果您只浏览网站很容易错过:http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/essential/io/examples/ObjectStreams.java

答案 2 :(得分:0)

就java中的Game Development而言,它是用户MVC的最佳方法  您也可能使用与您提到的不同的所有类和变量

100 is a normal level 
0 = Single 1 = Not very close, but dating 2 = close and dating 3 = engaged 4 = married
Tests if the player is injured, 1-5 on severeness
Money status: 1 is poor, 5 is rich

要使用它们,你需要在类中使用一些getter和setter,这样如果你对所有具有不同Beans类的bean使用Bean和一个单独的包,它就会更好。 javadocCollections 为了使用对象

来使用和映射数据

Saving Variables in game