Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6824465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:54:17+00:00 2026-05-26T21:54:17+00:00

Here is what I do when I save my score to the highscore. public

  • 0

Here is what I do when I save my score to the highscore.

public void save() {
    try {
        BufferedWriter fw = new BufferedWriter(new FileWriter(myDir
                + "/HIGHSCORE.txt"));
        for (Integer i : scores) {
            System.out.println(i);
            fw.write(i);
            fw.newLine();
            fw.flush();
        }
        fw.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void beforeSave() {
    List<String> stringReader = new ArrayList<String>();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(myDir + "/HIGHSCORE.txt"));
        String s = "";
        while ((s = br.readLine()) != null) {
            System.out.println("LINE: " + s + "\n");
            scores.add(Integer.parseInt(s));
        }
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

I have two methods to save and load the data information, beforeSave() do I use in the onResume() mwthod, and save() have I put in the onPause(). It crash when I call the onStop()(shut down the program), And I’m prette sure that befoeStart() doesn’t work eather. I have use the Scanner class, to use the method nextInt(), but it doensn’t work :(. And when I just save one score, it workds. What on earth do I do wrong?

PS. I’m now sure if I have to use flush(), but it doesn’t work befoe eather.

//Daniel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:54:17+00:00Added an answer on May 26, 2026 at 9:54 pm

    Change fw.write(i); to fw.write(i.toString());

    Rationale:
    Currently your code saves values into a file in binary format but reads them in text format. You have to use the same format to fix the issue. Saving in text format makes reading highscores.txt much easier so I provided that solution.

    Here’s my test code:

    package com.me;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    
    public class Main extends Activity
    {
        private static String FILE_NAME = "HIGHSCORES.txt"; // Added this for better maintainability
        private String myDir;
        private List<Integer> scores;
    
    
        @Override public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            // Folder in SD card
            myDir = Environment.getExternalStorageDirectory() + "/TwoDee"; // Just a random name
    
            // Initialize Integer list with some values
            scores = new ArrayList<Integer>();
            scores.add(new Integer(5));
            scores.add(new Integer(9));
            scores.add(new Integer(11));
    
            // Make sure there's the file or our test crashes. I hope you have similar code elsewhere?
            try
            {
                File folder = new File(Environment.getExternalStorageDirectory(), "TwoDee");
                if (folder.exists() == false)
                    folder.mkdirs();
    
                File file = new File(folder, FILE_NAME);
                if (file.exists() == false)
                    file.createNewFile();
            }
            catch (Exception e)
            {
                System.out.println("Fail"); // Blunt :)
            }
        }
    
        @Override public void onResume()
        {
            beforeSave();
            super.onResume();
        }
    
        @Override public void onPause()
        {
            save();
            super.onPause();
        }
    
        @Override public void onStop()
        {
            super.onStop();
        }
    
        public void save()
        {
            Log.v("TwoDee", "save...");
            try
            {
                BufferedWriter fw = new BufferedWriter(new FileWriter(myDir + "/" + FILE_NAME));
                for (Integer i : scores)
                {
                    Log.v("TwoDee", i.toString());
                    fw.write(i.toString());
                    fw.newLine();
                    //fw.flush(); // This is not really needed.
                }
                fw.close();
    
            } catch (FileNotFoundException e)
            {
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
    
            Log.v("TwoDee", "...save");
        }
    
        public void beforeSave()
        {
            //List<String> stringReader = new ArrayList<String>(); // Not needed...
            BufferedReader br = null;
            Log.v("TwoDee", "beforeSave...");
            try
            {
                br = new BufferedReader(new FileReader(myDir + "/" + FILE_NAME));
                String s = "";
                while ((s = br.readLine()) != null)
                {
                    Log.v("TwoDee", s);
                    //System.out.println("LINE: " + s + "\n"); // Not needed...
                    scores.add(Integer.parseInt(s));
                }
            } catch (FileNotFoundException e1)
            {
                e1.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
    
            Log.v("TwoDee", "...beforeSave");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
Here is an example of what causes the error: ruby-1.9.2-p290 :004 > Post.new(title: new).save!
I have a score system and I would like to log all scores in
OK, my need here is to save whatever typed in the rich text box
Here's a common, simple task: Read configuration settings from a configuration file, save the
I want to download and save pdf file to internal storage. Here is code
What am I doing wrong here? Im trying to save a canvas drawing by
To use a contrived example in Java, here's the code: enum Commands{ Save(S); File(F);
New to C# here, and I'm trying to store a string in a Setting.
I'm trying to save a logged in user's score to my sql database. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.