开始读取数据:
public static void start(){
Reader input = null;
BufferedReader br = null;
try {
input = new FileReader("record/MyRecord.txt");
br = new BufferedReader(input);
String str = br.readLine();
if(str == null){
str ="0";
}
score = Integer.parseInt(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最后保存数据:
public static void end(){
Writer out = null;
BufferedWriter bw = null;
try {
out = new FileWriter("record/MyRecord.txt");
bw = new BufferedWriter(out);
bw.write(score+"\r\n");
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}