Android Studio : Error : Stream Closed
I try to create a text file to save my options but when I launch the creation of the file, I get an error: Stream Closed.
My class file :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
public class SettingsFile {
private Settings settings;
private File file;
public SettingsFile(Settings settings, File file) {
this.settings = settings;
this.file = new File(file, "options.txt");
loadOptions();
}
public void loadOptions() {
try {
if(!file.exists()) {
saveOptions();
}
BufferedReader reader = new BufferedReader(new FileReader(this.file));
String s = "";
while((s = reader.readLine()) != null) {
try {
String[] content = s.split(":");
if(content[0].equals("PLAYER1")) {
Settings_enum.PLAYER1.setString_value(content[1]);
}
if(content[0].equals("PLAYER2")) {
Settings_enum.PLAYER2.setString_value(content[1]);
}
} catch (Exception e) {
System.err.println("[FILE ERROR1]" + e);
}
reader.close();
}
} catch (Exception e) {
System.err.println("[FILE ERROR2]" + e);
}
}
public void saveOptions() {
try {
PrintWriter print = new PrintWriter(new FileWriter(this.file));
print.println("Settings File");
print.println("PLAYER1:" + Settings_enum.PLAYER1.getString_value());
print.println("PLAYER2:" + Settings_enum.PLAYER2.getString_value());
print.close();
} catch(Exception e) {
System.err.println("[FILE ERROR3]" + e);
}
}
} |
My class Activity :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
public class Settings extends AppCompatActivity {
public ImageButton close;
public Button valid_button1;
public SettingsFile settingsFile;
public File DataDir;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
this.settingsFile = new SettingsFile(this, getExternalCacheDir());
this.close = findViewById(R.id.close2);
this.valid_button1 = findViewById(R.id.valid_button1);
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Settings_enum player1 = Settings_enum.PLAYER1;
Settings_enum player2 = Settings_enum.PLAYER2;
System.out.println(player1.getString_value());
System.out.println(player2.getString_value());
settingsFile.saveOptions();
Intent otherActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(otherActivity);
finish();
}
});
valid_button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText champ_1 = findViewById(R.id.champ_joueur1);
EditText champ_2 = findViewById(R.id.champ_joueur2);
String val1 = champ_1.getText().toString();
String val2 = champ_2.getText().toString();
Settings_enum player1 = Settings_enum.PLAYER1;
Settings_enum player2 = Settings_enum.PLAYER2;
player1.setString_value(val1);
player2.setString_value(val2);
System.out.println(player1.getString_value());
System.out.println(player2.getString_value());
settingsFile.saveOptions();
}
});
} |
My class EnumOptions :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
public enum Settings_enum {
PLAYER1("Joueur 1"),
PLAYER2("Joueur 2");
private String string_value;
private int int_value;
private boolean boolean_value;
Settings_enum(String str){
this.string_value = str;
}
Settings_enum(int int_value){
this.int_value = int_value;
}
Settings_enum(boolean boolean_value){
this.boolean_value = boolean_value;
}
public void setString_value(String string_value) {
this.string_value = string_value;
}
public String getString_value() {
return string_value;
}
public void setInt_value(int int_value) {
this.int_value = int_value;
}
public int getInt_value() {
return int_value;
}
public void setBoolean_value(boolean boolean_value) {
this.boolean_value = boolean_value;
}
public boolean isBoolean_value() {
return boolean_value;
}
} |
If someone finds a solution I am interested! Thank you :)