Read the five grades in a text file, then print them with the new grade added to the list - ANDROID

In my program, when a player submits a score, it is added to a local text file named localHighScores

. This is a list of the top five points achieved by the player on that particular device.

I wasn't sure how to write to a newline using FileOutputStream

(if you know, please share), so I put a space between each score instead. So what I am trying to do is when the player clicks submit

, the program will open the file and read all the current data. It will save it in String Array

, each of which will be one of five scores in the text file, and when it hits the "space" in fie, it will add the score just read to the entry array element

The code I have is the following:

    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();            
    }

      

You will now notice that this will not result in a score if a new record is reached. What I plan to do next. At the moment I'm just trying to get the program to do the main thing that reads in the current data, insert it into an array, and then print it back to that file along with a new score

Any ideas how this might work as it currently does not print anything even when I have a score in a text file before starting

+3


source to share


2 answers


I'm only a freshman in Java programming and I'm a new user here on stackoverflow.com so excuse me if there are special rules for coding for android that I am not aware of, which prevents this simple and humble example from working. But this is how I could read from a file in the simplest way possible.

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++;
}

      

As for writing to a file? Put it in a completely different method and just create a simplified method like toString that outputs whatever values ​​they want in the file and then create a similar method loadToFile and use the string method to print back to the file with a print stream, something like below.



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.

      

Again, if this is something you already know, which is simply not possible in this context, please ignore me, but if not, I hope this was helpful. As far as exceptions are concerned, you can understand that you yourself.;)

+1


source


Since you are a beginner and I am assuming you are trying to get things out as quickly as possible, I would recommend using SharedPreferences

. It's basically just a huge standing card for you! Having said that ... you really should learn about all storage methods on Android, so check out this doc:

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



Android docs are awesome! FYI SharedPreferences may not be the best and finest way to do this ... but I'm all about rapid prototyping as a student. If you want, write a wrapper class around SharedPreferences.

0


source







All Articles