读写SD卡列表

发布于 2024-12-28 12:24:58 字数 1881 浏览 3 评论 0原文

我一直在谷歌搜索,我看过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你是我的挚爱i 2025-01-04 12:24:58

您有几个选项

  • 共享首选项: 将私有原始数据存储在键值对中。存储少量简单数据就可以了。它可以满足您的需求,具体取决于您需要存储的数据的大小。
  • 内部存储:将私有数据存储在设备内存上。我认为这是最好的选择,因为数据是私有的,其他应用程序无法访问。
  • 外部存储:将公共数据存储在共享外部存储上。这就是你正在尝试做的事情。它会起作用,但我认为这不是一个好主意,因为用户可以更改文件并破解游戏。
  • SQLite数据库:将结构化数据存储在私有数据库中。在您的情况下,数据很简单并且并不真正需要关系存储,这可能是一种矫枉过正的情况。
  • 网络连接

在任何情况下,请尝试使用可简化进一步处理的文件格式,例如,您可以使用 Properties 文件(易于用 Java 编写/解析),并将每个级别检查点存储为属性或类似的东西(即level_1_checkpoint=200)。

You have several options:

  • Shared preferences: store private primitive data in key-value pairs. It's ok to store a small amount of simple data. It could fit your needs depending of the size of the data you need to store.
  • Internal Storage: store private data on the device memory. I think it is the best choice because the data is private and can not be accessed by other applications.
  • External Storage: store PUBLIC data on the shared external storage. This is what you are trying to do. It will work but I think it is not a good idea because the user could change the file and hack the game.
  • SQLite Databases: store structured data in a private database. Probably an overkill in your case, where the data is simple and a relational storage is not really needed.
  • Network Connection

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文