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 
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.
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: