【问题标题】:Read the five score in a textfile then print them back with the new score added to the list - ANDROID读取文本文件中的五个乐谱,然后将它们打印回来,并将新乐谱添加到列表中 - ANDROID
【发布时间】:2023-04-06 03:00:02
【问题描述】:

在我的程序中,当玩家提交分数时,它会被添加到名为localHighScores 的本地文本文件中。这是玩家在该特定设备上获得的前五名的列表。

我不确定如何使用FileOutputStream 写入新行(如果您知道请分享),所以我在每个分数之间输入了一个空格。因此我想要做的是当播放器点击submit 时,程序将打开文件并读取保存的任何当前数据。它会将它保存到String Array,每个元素都是文本文件中的五个分数之一,当它在文件中击中一个“空格”时,它会将刚刚读取的分数添加到写入数组元素中

我目前的代码如下:

    String space = "  ";
    String currentScoreSaved;

    String[] score = new String[5]; 

    int i = 0;
    try
    {           
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput("localHighScore.txt")));
        String inputString;StringBuffer stringBuffer = new StringBuffer();

        while ((inputString = inputReader.readLine()) != null && i < 6)
        {
            if((inputString = inputReader.readLine()) != space)
            {
                stringBuffer.append(inputString + "\n");
                i++;
                score[i] = stringBuffer.toString();
            }
        }
        currentScoreSaved = stringBuffer.toString();
        FileOutputStream fos = openFileOutput("localHighScore.txt", Context.MODE_PRIVATE);

        while (i < 6)
        {
            i++;

            fos.write(score[i].getBytes());
            fos.write(space.getBytes());

        }       
        fos.write(localHighScore.getBytes());
        //fos.newLine(); //I thought this was how you did a new line but sadly I was mistaken

        fos.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();            
    }

现在您会注意到,如果达到新的高分,这不会重新排列分数。我计划下一步做的事情。目前我只是想让程序执行在当前数据中读取的主要内容,将其粘贴在一个数组中,然后将其与新分数一起打印回该文件

关于这可能如何工作的任何想法,因为目前即使我事先在文本文件中有分数,它也没有打印任何内容

【问题讨论】:

  • 为什么选择将信息存储在文本文件中?
  • 因为我认为这对于项目的这一部分来说是最简单的,而且我对 Android 缺乏了解

标签: java android eclipse text-files fileoutputstream


【解决方案1】:

我只是 Java 编程的一年级学生,而且我是 stackoverflow.com 的新用户,如果 android 编码有一些我不知道的特殊规则,请原谅我,这会阻止这种简单和谦逊工作中的例子。但这是我以最简单的方式从文件中读取的方式。

File tempFile = new File("<SubdirectoryIfAny/name_of_file.txt");

Scanner readFile = new Scanner( tempFile );

// Assuming that you can structure the file as you please with fx each bit of info
// on a new line.

int counter = 0;
while ( readFile.hasNextLine() ) {
    score[counter] = readFile.nextLine();
    counter++;
}

至于写回文件?将它放在一个完全不同的方法中,并简单地制作一个简化的 toString 类方法,以您希望它们在文件中的确切方式打印出所有值,然后创建一个类似“loadToFile”的方法并使用 to string 方法打印回来使用打印流进入文件,如下所示。

File tempFile = new File("<SubdirectoryIfAny/name_of_file.txt");

PrintStream write = new PrintStream(tempFile);

// specify code for your particular program so that the toString method gets the 
// info from the string array or something like that.

write.print( <objectName/this>.toStringLikeMethod() );
// remember the /n /n in the toStringLikeMethod so it prints properly in the file.

同样,如果这是您已经知道的事情,在这种情况下这是不可能的,请忽略我,但如果不是,我希望它有用。至于例外情况,您可以自己计算。 ;)

【讨论】:

  • 非常感谢您的工作。它仍然会导致错误,但那是因为对于File tempFile = new File("&lt;SubdirectoryIfAny/name_of_file.txt"); 行,您需要像您所说的那样拥有文件目录。所以为了解决这个问题,我在你的代码之前写了File dir = getFilesDir();然后编辑你的行看起来像这样:File tempFile = new File(dir, "name_of_file.txt");
  • 这也有效,您只需完全删除
  • 文件("SubDirName/name_of_file.txt")
【解决方案2】:

由于您是初学者,并且我假设您正在尝试尽快完成任务,因此我建议您使用SharedPreferences。基本上它只是一个供您使用的巨大持久地图!话虽如此...您应该真正了解Android中的所有存储方式,因此请查看此文档:

http://developer.android.com/guide/topics/data/data-storage.html

Android 文档很棒!仅供参考,SharedPreferences 可能不是做到这一点的最好和最棒的方法......但我完全赞成作为学习者的快速原型设计。如果需要,可以围绕 SharedPreferences 编写一个包装类。

【讨论】:

    猜你喜欢
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多