以下是我将得分保存到高分榜时的所作所为。
public void save() {
try {
BufferedWriter fw = new BufferedWriter(new FileWriter(myDir
+ "/HIGHSCORE.txt"));
for (Integer i : scores) {
System.out.println(i);
fw.write(i);
fw.newLine();
fw.flush();
}
fw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void beforeSave() {
List<String> stringReader = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(myDir + "/HIGHSCORE.txt"));
String s = "";
while ((s = br.readLine()) != null) {
System.out.println("LINE: " + s + "\n");
scores.add(Integer.parseInt(s));
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我有两种保存和加载数据信息的方法,在onResume()mwthod中使用beforeSave(),而在onPause()中使用save()。当我调用onStop()(关闭程序)时它会崩溃,而且我确定befoeStart()不能正常工作。我使用Scanner类,使用方法nextInt(),但它不起作用:(。当我只保存一个分数时,它会工作。我究竟做错了什么?
PS。我现在确定我是否必须使用flush(),但它不适用于befoe eather。
//丹尼尔
答案 0 :(得分:3)
将fw.write(i);
更改为fw.write(i.toString());
理由:
目前,您的代码会将值以二进制格式保存到文件中,但会以文本格式读取它们。您必须使用相同的格式来解决问题。以文本格式保存使得阅读highscores.txt变得更加容易,因此我提供了该解决方案。
这是我的测试代码:
package com.me;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class Main extends Activity
{
private static String FILE_NAME = "HIGHSCORES.txt"; // Added this for better maintainability
private String myDir;
private List<Integer> scores;
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Folder in SD card
myDir = Environment.getExternalStorageDirectory() + "/TwoDee"; // Just a random name
// Initialize Integer list with some values
scores = new ArrayList<Integer>();
scores.add(new Integer(5));
scores.add(new Integer(9));
scores.add(new Integer(11));
// Make sure there's the file or our test crashes. I hope you have similar code elsewhere?
try
{
File folder = new File(Environment.getExternalStorageDirectory(), "TwoDee");
if (folder.exists() == false)
folder.mkdirs();
File file = new File(folder, FILE_NAME);
if (file.exists() == false)
file.createNewFile();
}
catch (Exception e)
{
System.out.println("Fail"); // Blunt :)
}
}
@Override public void onResume()
{
beforeSave();
super.onResume();
}
@Override public void onPause()
{
save();
super.onPause();
}
@Override public void onStop()
{
super.onStop();
}
public void save()
{
Log.v("TwoDee", "save...");
try
{
BufferedWriter fw = new BufferedWriter(new FileWriter(myDir + "/" + FILE_NAME));
for (Integer i : scores)
{
Log.v("TwoDee", i.toString());
fw.write(i.toString());
fw.newLine();
//fw.flush(); // This is not really needed.
}
fw.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
Log.v("TwoDee", "...save");
}
public void beforeSave()
{
//List<String> stringReader = new ArrayList<String>(); // Not needed...
BufferedReader br = null;
Log.v("TwoDee", "beforeSave...");
try
{
br = new BufferedReader(new FileReader(myDir + "/" + FILE_NAME));
String s = "";
while ((s = br.readLine()) != null)
{
Log.v("TwoDee", s);
//System.out.println("LINE: " + s + "\n"); // Not needed...
scores.add(Integer.parseInt(s));
}
} catch (FileNotFoundException e1)
{
e1.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
Log.v("TwoDee", "...beforeSave");
}
}