📄 loadandsave.java
字号:
package model;
import java.awt.Point;
import java.io.*;
import java.util.Date;
import java.util.Properties;
/**
* 这个类实现了在游戏结束时的数据保存,保存的内容包括最短破关时间纪录,上次退出时主窗口位置,存档游戏.
*
* @author : 李强,何明,何晓飞
* @version : 2.0
*
*/
public class LoadAndSave {
/* 保存窗口位置的一个属性集 */
private static final Properties prop1 = new Properties();
/* 保存存档游戏的数据的一个属性集 */
private static final Properties prop2 = new Properties();
/* 保存最短破关时间纪录的文件 */
public static File recordFile = new File("record.dat");
/* 保存上次退出时主窗口位置的文件 */
public static File setFile = new File("setup.ini");
/* 保存存档游戏的数据的文件 */
public static File saveFile = new File("save.ini");
/**
* 通过读取保存最短破关时间纪录的文件,获取其中保存的时间.
*
* @return seconds 返回最短破关时间纪录的文件中保存的时间
*/
public static int getRecord() {
int seconds = 400;// 记录从文件中获得的时间
/* 读取文件 */
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader(
new FileInputStream(recordFile)));// 输入流
String line = null;
while ((line = bReader.readLine()) != null)
try {
String[] values = line.split("\t");// 根据给定的正则表达式的匹配来拆分从文件中读入的字符串、
int s = Integer.parseInt(values[1]);
if (s < seconds)
seconds = s;
} catch (RuntimeException e) {
}
bReader.close();
} catch (Exception e) {
}
return seconds;// 返回时间
}
/**
* 从保存窗口位置的文件读入窗口位置 并保存到属性集prop1中.
*
*/
public static void load() {
try {
prop1.loadFromXML(new FileInputStream(setFile));
} catch (Exception e) {
}
}
/**
* 从保存窗口位置的属性集prop1中获得窗口的两属性x坐标,y坐标, 并用x,y生成一个Point对象
*
* @return location 由x,y生成一个Point对象
*/
public static Point loadLocation() {
Point location = null;
load();
int x = 0;
int y = 0;
try {
x = Integer.parseInt(prop1.getProperty("frameX"));
y = Integer.parseInt(prop1.getProperty("frameY"));
} catch (NumberFormatException e) {
}
location = new Point(x, y);
return location;
}
/**
* 将表示窗口位置x,y坐标保存到文件中
*
*/
public static void save() {
try {
prop1.storeToXML(new FileOutputStream(setFile), "no comment");
} catch (Exception e) {
}
}
/**
* 根据所给窗口位置将其x,y属性写进保存窗口位置的属性集prop1中, 调用save()保存到文件中
*
* @param p
* 表示窗口位置Point对象
*/
public static void saveLocation(Point p) {
prop1.setProperty("frameX", p.x + "");
prop1.setProperty("frameY", p.y + "");
save();
}
/**
* 将参数传入的时间保存到保存最短破关时间纪录的文件
*
* @param seconds
* 要写入文件的时间
*/
public static void saveRecord(int seconds,int level) {
try {
FileOutputStream fos = new FileOutputStream(recordFile, true);
PrintWriter out = new PrintWriter(fos, true);
out.println(new Date(System.currentTimeMillis()) + "第"+level+"关所创的纪录是\t"
+ seconds + "\t" + "秒");
out.close();
} catch (FileNotFoundException e) {
}
}
/**
* 从保存存档游戏的数据的文件读入存档游戏的数据 并保存到属性集prop2中.
*
* @return boolean 读取文件成功返回true,失败返回false
*/
public static boolean loadGame() {
try {
prop2.loadFromXML(new FileInputStream(saveFile));// 将指定输入流中由 XML
// 文档所表示的所有属性加载到此属性表中
} catch (IOException e) {
return false;
}
return true;
}
/**
* 从保存存档游戏数据的属性集prop2中获得存档游戏的数据pictureFile,gameMusic,level
* 并调用Model的三个方法对其对应三个属性进行设置
*
* @param model
* 需要获得止三个属性的Model对象
*
* @return level 从属性集prop2中获得存档游戏的数据成功返回level值,失败返回0
* 调用方法loadGame()产生的返回值是false时也返回0
*/
public static int loadGameData(Model model) {
String pictureFile = "";
String gameMusic = "";
int level = 0;
boolean b = loadGame();
if (b == true) {
try {
pictureFile = prop2.getProperty("pictureFile");
gameMusic = prop2.getProperty("gameMusic");
level = Integer.parseInt(prop2.getProperty("level"));
} catch (NumberFormatException e) {
return 0;
}
model.setPictureFile(pictureFile);// 设置Model的PictureFile属性
model.setMusicFile(gameMusic);// 设置Model的MusicFile属性
model.setLevel(level);// 设置Model的level属性
return level;
}
return 0;
}
/**
* 将游戏数据pictureFile,pictureFile,level保存到文件中
*
*/
public static void saveGame() {
try {
// 发出一个表示此表中包含的所有属性的 XML 文档
prop2.storeToXML(new FileOutputStream(saveFile), "no comment");
} catch (Exception e) {
}
}
/**
* 根据所给游戏的数据将它们写进保存存档游戏数据的属性集prop2中, 并调用saveGame()保存到文件中
*
* @param p
* pictureFile(游戏中的图片)
* @param m
* pictureFile(游戏中的音乐)
* @param l
* level(游戏关数)
*/
public static void saveGameData(String p, String m, int l) {
prop2.setProperty("pictureFile", p + "");
prop2.setProperty("gameMusic", m + "");
prop2.setProperty("level", l + "");
saveGame();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -