I have a app that every time you open it, the app reads a file saved into the internal storage and prints something. Then it updates the file.
String filename = "myfile";
String restore = "";
FileOutputStream outputStream;
FileInputStream inputStream;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
restore = read();
System.out.println("Before if statement the restore value is: " + restore);
if(restore == "test3"){
restore = "test4";
System.out.println("I' m saving: " +restore);
save(restore);
}
if(restore == "test2"){
restore = "test3";
System.out.println("I'm saving: " +restore);
save(restore);
}
if(restore2 == "test1"){
restore = "test2";
System.out.println("I'm saving " +restore);
save(restore);
}
}
public String read() {
try {
inputStream = openFileInput(filename);
StringBuilder builder = new StringBuilder();
String restore2 = null;
int ch;
while((ch = inputStream.read()) != -1){
builder.append((char)ch);
}
System.out.println(builder.toString());
if(builder.toString() != null) {
restore2 = builder.toString();
System.out.println("Restore 2 value is: " + restore2);
}
inputStream.close();
return restore2;
} catch (Exception e) {
e.printStackTrace();
return "test1";
}
}
private void save(String salvare) {
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(salvare.getBytes());
System.out.println("I saved: " + salvare);
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
When I open the application for the first time, everything goes well and I get:
Before if statement the restore value is test1
I'm saving test1
I saved test 1
When I reopen my app, The read() function works well because I get
Before if statement the restore value is test2
But I don't get I'm saving test2
. This means that the second if statement seems to be useless. Looking at my log cat I don't get any type of error. What can be the problem?