• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

data not saving in file properly

nick2361

Lurker
Hello :) Hope you're having a nice day. As for the question, I'm trying to make an android counter app, which will save count state in file and read it on app open, but every time I start the app, it shows 0. Any help is welcome :)

Code:
    package com.UntitledStudios.Counter;

    import android.content.Context;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.Button;
    import android.view.View;
    import android.widget.TextView;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Scanner;

    public class MainActivity extends AppCompatActivity{

       int count;

       Button XuinaButton;
       [USER=1021285]@override[/USER]
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);

           File file = new File("COUNTER");
           if(file.exists()) {
               try {
                   FileInputStream in = openFileInput("COUNTER");
                   Scanner scanner = new Scanner(in);
                   scanner.nextInt(count);
               } catch (FileNotFoundException e) {
                   e.printStackTrace();
               }
           }
           else{
               count =0;
           }

           CounterButton = (Button) findViewById(R.id.Counter);

           CounterButton.setOnClickListener(new View.OnClickListener() {
               [USER=1021285]@override[/USER]
               public void onClick(View v) {
                   count++;
                   TextView CounterText = ((TextView) findViewById(R.id.CurrentCount));
                   CounterText.setText("" + count);
               }

           });


           FileOutputStream outputStream;

           try {
               String Counted = Integer.toString(count);
               outputStream = openFileOutput("COUNTER.txt", Context.MODE_PRIVATE);
               outputStream.write(Counted.getBytes());
               outputStream.close();
           } catch (Exception e) {
               e.printStackTrace();
           }
       }

    }

I have root access on my asus, so I can check /data/data/com.UntitledStudios.Counter/files/Counter.txt and see the value written inside.
 
Last edited by a moderator:
Good evening. Your code probably catches an exception but you're not seeing it. This is a very bad idea

Code:
  e.printStackTrace();

Because if an exception is caught, then printStackTrace() will send the output to standard out, and for an app, who knows where that is. You probably won't see it in your Logcat view.

Instead write this

Code:
  throw e;

The exception will be caught by the system and your app will terminate. But you'll see the whole stack trace in your Logcat view. This should tell you exactly what went wrong.

Or you could use

Code:
  Log.d("myApp", e.getMessage());
 
Back
Top Bottom