读写SD卡列表
我一直在谷歌搜索,我看过 StackOverflow 并看到很多例子,但我无法真正掌握它。我是 Android 新手,正在学习如何读/写。
也许你们中的一个人可以帮助我编写一些简单的代码。我想要的是将分数、剩余时间和通过的检查点保存在文件中,因为您可以在几天后停止并再次玩。
所以在我的想象中,理想的情况是(因为它只是简单的数据)不使用 SQL lite,而只使用文本文件。我真的找不到如何使用列表、读取、写入的示例代码。
如果您从文件中读取整个列表(字符串),那就太好了,该列表的长度可以改变。然后您可以更新列表,假设 list[5]=200(原为 500),然后将其写回到文件中。此外,您可能想向文件添加数据(通过的检查点)。
作为开始,我想出了这个,但我被检查点困住了,不确定我是否写入文件,整个文件将被重写,或者我正在添加数据:
private static String filename = "currentgamedata.txt";
private String myDir = Environment.getExternalStorageDirectory() + "/MyDirectory/";
String enter = System.getProperty( "line.separator" );
public void WriteCurrentGameData(){
try {
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(openFileOutput( myDir+filename , MODE_WORLD_WRITEABLE)));
String MinutesLeft=Long.toString(GameTimeLeft);
String SaveScore=Integer.toString(score);
writer.write(MinutesLeft+enter);
writer.write(SaveScore+enter);
//HERE SHOULD COME SOME CODE FOR THE CHECKPOINTS
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void ReadCurrentGameData(){
try {
BufferedReader input = new BufferedReader( new InputStreamReader(
openFileInput( myDir+filename )));
GameTimeLeft=Long.parseLong(input.readLine());
score=Integer.parseInt(input.readLine());
Log.d( "Reader" , Long.toString(GameTimeLeft) );
Log.d( "Reader" , Integer.toString(score) );
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
抱歉,代码怎么不出来非常漂亮。所以总结一下问题:
- 如果我想要写入和读取游戏数据,应该使用什么样的方法,为什么? (像这里的 FE 或 SQLlite?)
- 我正在获取 myDir,这是存储此类数据的最佳位置,还有其他位置以及为什么?
- 有没有办法使用 ArrayList?有人有一个简单的例子吗?
多谢!
I´ve been googling, I´ve looked at StackOverflow and see a lot of examples, but I can´t really get a hold of it. I´m new to android and learning how to read/write.
Maybe one of you can help me with some simple code. What I want is to save the score, time left and passed checkpoints in a file, since you can stop and play again days later.
So in my imagination, ideal would be (since it´s only simple data) not to work with SQL lite, but just with a textfile. I couldn´t really find examlpe code of how to work with a list, read, write.
It would be nice if you read an entire list (strings) from the file, this list can change in length. Then you can update the list, let´s say list[5]=200 (was 500) and then write it back to the file. Also you might want to add data to the file (the passed checkpoints).
As a start I came up with this, but I´m stuck with the checkpoints and am not sure if I write to the file the entire file will be rewritten or I´m adding data:
private static String filename = "currentgamedata.txt";
private String myDir = Environment.getExternalStorageDirectory() + "/MyDirectory/";
String enter = System.getProperty( "line.separator" );
public void WriteCurrentGameData(){
try {
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(openFileOutput( myDir+filename , MODE_WORLD_WRITEABLE)));
String MinutesLeft=Long.toString(GameTimeLeft);
String SaveScore=Integer.toString(score);
writer.write(MinutesLeft+enter);
writer.write(SaveScore+enter);
//HERE SHOULD COME SOME CODE FOR THE CHECKPOINTS
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void ReadCurrentGameData(){
try {
BufferedReader input = new BufferedReader( new InputStreamReader(
openFileInput( myDir+filename )));
GameTimeLeft=Long.parseLong(input.readLine());
score=Integer.parseInt(input.readLine());
Log.d( "Reader" , Long.toString(GameTimeLeft) );
Log.d( "Reader" , Integer.toString(score) );
input.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Sorry, somhow the code doesn´t come out very beautifully. So summarized the questions:
- What kind of method should I use if I want the game data written and read and why? (FE like up here or SQLlite?)
- I´m getting the myDir, is this the best location to store such data, where else and why?
- Is there a way to work with an ArrayList? Does anybody have a simple example for that?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有几个选项:
在任何情况下,请尝试使用可简化进一步处理的文件格式,例如,您可以使用 Properties 文件(易于用 Java 编写/解析),并将每个级别检查点存储为属性或类似的东西(即level_1_checkpoint=200)。
You have several options:
In any case, try to use a file format that simplifies further processing, for example you could use a Properties file (easy to write/parse in Java), and store each level checkpoint as a property or something like that (i.e. level_1_checkpoint=200).