I'm currently making an app that needs to save the number and write it into a text file. But every time it writes to the file. It overwrites the last things saved. Here is the code of the entire program in java.
package com.galaxy.nestetrisscores;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.io.FileOutputStream;
import java.io.File;
import android.view.View;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
private Button button;
private EditText editNumber;
private String file = "Scores.txt";
private String fileContents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editNumber = findViewById(R.id.editNumber);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fileContents = editNumber.getText().toString();
try {
FileOutputStream fOut = openFileOutput(file, MODE_PRIVATE);
fOut.write(fileContents.getBytes());
fOut.close();
File fileDir = new File(getFilesDir(), file);
Toast.makeText(getBaseContext(), "File Saved at" + fileDir, Toast.LENGTH_LONG).show();
} catch(Exception e) {
e.printStackTrace();
}
}
});
}
}