Dean Ayalon Dean Ayalon -4 years ago 49
Java Question

Suddenly can't read/write to file

I'm making an android application, which saves data to a file in the settings activity.

I made some custom functions to ease writing my files, they're in a class all my activities inherit from, including the settings activity.

Custom functions:

public void WriteToFile(String filename, String tag, String value) {
try {
FileOutputStream fileOut = openFileOutput(filename + ".txt", MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fileOut);
writer.write(ReadFile(filename + ".txt") + tag + ":" + value + ";");
writer.close();
} catch (Exception e) {
Toast.makeText(this, "ERROR: " + e.toString(), Toast.LENGTH_LONG).show();
Log.e("Error writing: ", e.toString());
}
}

public void WipeFile(String filename) {
try {
FileOutputStream fileOut = openFileOutput(filename + ".txt", MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fileOut);
writer.write("");
writer.close();
} catch (Exception e) {
Toast.makeText(this, "ERROR: " + e.toString(), Toast.LENGTH_LONG).show();
Log.e("Error writing: ", e.toString());
}
}

public String ReadFile(String filename) {
try {
FileInputStream fileIn = openFileInput(filename + ".txt");
InputStreamReader InputRead = new InputStreamReader(fileIn);
char[] inputBuffer = new char[10000];
String content = "", readString;
int charRead;
while ((charRead = InputRead.read(inputBuffer)) > 0) {
readString = String.copyValueOf(inputBuffer, 0, charRead);
content += readString;
}
InputRead.close();
return content;
} catch (Exception e) { WipeFile(filename); return ""; }
}

public String FileValue(String filename, String tag, String defaultValue) {
String[] content = ReadFile(filename + ".txt").split(";");
for (String pair : content) {
if (pair.split(":")[0].equals(tag)) return pair.split(":")[1];
} WriteToFile(filename, tag, defaultValue); return defaultValue;
}


Settings activity:

@Override
@SuppressWarnings("ConstantConditions")
protected void onCreate(Bundle savedInstanceState) {
ToolbarTitle = "Settings";
ActivityID = R.layout.activity_settings;
ToolbarID = R.id.settings_toolbar;
ToolbarIcon = R.mipmap.settings_icon;
ActivityLayout = R.id.settings_layout;
super.onCreate(savedInstanceState);

if (prefs.getInt("LoggedinID", 0) == 0) findViewById(R.id.settings_user).setVisibility(View.GONE);

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.settings_lowBattery, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
int spinnerPosition = adapter.getPosition(FileValue("settings", "Alert", "20%"));
Spinner battery = ((Spinner) findViewById(R.id.settings_battery));
battery.setAdapter(adapter);
battery.setSelection(spinnerPosition);

((Switch) findViewById(R.id.settings_notifications)).setChecked(FileValue("settings", "Notifications", "1").equals("1"));

findViewById(R.id.settings_ads).setVisibility((FileValue("settings", "Ads", "1").equals("1") ? View.VISIBLE : View.INVISIBLE));
}

@SuppressWarnings("ConstantConditions")
public void Apply(View view) {
WipeFile("settings");
WriteToFile("settings", "Notifications", (((Switch) findViewById(R.id.settings_notifications)).isChecked() ? "1" : "0"));
WriteToFile("settings", "Alert", ((Spinner) findViewById(R.id.settings_battery)).getSelectedItem().toString());
}

public void Ads(View view) {
Toast.makeText(this, "Just a prank, bro", Toast.LENGTH_SHORT).show();
WriteToFile("settings", "Ads", "0");
}


What's weird is that it all worked when it was messy and not in custom functions, any idea why?

The problem seems to occur in the ReadFile function, where InputRead.read(inputBuffer) returns -1 (No data in file).

I have no idea how to even check where the problem lies, when writing to the file or when reading from it....

Thanks ahead

Answer Source

It seems like the problem might be in how you are appending to file...

Internally your write function opens the file, then before closing it, your read function opens the same file and closes it. It could be that either the read function is failing when it tries to open the file because it is open, but not closed. OR it could be that the read function closing the file also closes it for the write function...

The problem seems to be that you want to append to the file in your write function, but you are implementing it poorly. You do not need to rewrite the contents to file. You just need to find the proper flag to open the file for appending.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download