Read the five partitions in a text file and then print them with the new score added to the list - ANDROID

advertisements

In my program when the player submits a score it gets added to a local text file called localHighScores. This is list of the top five score the player has achieved while on that specific device.

I wasn't sure how to write to a new line using FileOutputStream (if you know please share), so instead I've inputted a space in between each score. Therefore what I am trying to do is when the player clicks submit the program will open the file and read any current data is saved. It will save it to an String Array, each element being one of the five score in the text file and when it hits a 'space' in the fie it will add the score just read to the write array element

The code I currently have is as follows:

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

Now you will notice this doesn't re arrange the score if a new highscore is achieved. That I am planning on doing next. For the moment I am just trying to get the program to do the main thing which is read in the current data, stick it in an Array then print it back to that file along with the new score

Any Ideas how this might work, as currently it's printing out nothing even when I had score in the textfile before hand


I'm only a first year student in Java programming and I am a new user here at stackoverflow.com, so pardon me if coding for android has some special rules I don't know about, which prevents this simple and humble example from working. But here is how I would read from a file in the simplest of ways.

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 the writing back to the file? Put it in an entirely different method and simply make a simplified toString-like method, that prints out all the values the exact way you want them in the file, then create a "loadToFile" like method and use the to string method to print back into the file with a printstream, 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 just not possible in this context please ignore me, but if not I hope it was useful. As for the exceptions, you can figure that you yourself. ;)