How To Read Data From Text File Then Overwrite It With New Data?
Solution 1:
This might be due to a FileNotFoundException
when you'd read the file in the save method. When you try to update the file and write into it, you don't separate the try/catch
methods so you might have an exception at the reading part which prevents to continue the script and to update the file. Maybe this was printing in the Logcat but you haven't take a look.
So I'd suggest you to check if the file already exists and to separate the reading/writing parts.
When you first read the file to display it in TextView
, just check if it's created to avoid a background exception:
Filef=newFile(getFilesDir().toString() + "/stars.txt");
Log.v("", "Does the file exist? " + f.exists());
if (f.exists()) {
readFile();
}
You can see here where the file should be stored (in getFilesDir()
). The path is choosen by default when you use openFileInput(String)
, and it's:
data/data/com.package.name/files/
You have to keep in mind that you actually don't create /files
folder in your code and for example, I had to create it myself to work with the above method. (This can be only my device but you'd be aware this can prevent the file to be created.)
Now, there is no big changes in your reading method and this is how it looks:
privatevoidreadFile() {
try {
FileInputStreamfile= openFileInput("stars.txt");
InputStreamReaderinRead=newInputStreamReader(file);
BufferedReaderbuffReader=newBufferedReader(inRead);
StringBufferstringBuffer=newStringBuffer();
String star;
while ((star = buffReader.readLine()) != null) {
stringBuffer.append(star);
}
inRead.close();
file.close();
txt_stars.setText(stringBuffer.toString());
} catch (Exception e) {
Log.e("ReadFile", e.toString());
}
}
Obviously, this method is called only if the previous check returns true
. You have to do the same when the Button
is clicked: check if it exists -> if yes, get the content, do your stuff (add, sum, etc) and write into it -> if not, just create it by writing into it.
Something as follows will work:
publicvoidwriteFile(View v) {
Filef=newFile(getFilesDir().toString() + "/stars.txt");
Log.v("", "Does it exist? " + f.exists());
Stringresult="";
if ( f.exists() ) {
try {
FileInputStreamfile= openFileInput("stars.txt");
InputStreamReaderinRead=newInputStreamReader(file);
BufferedReaderbuffReader=newBufferedReader(inRead);
StringBufferstringBuffer=newStringBuffer();
String star;
while ((star=buffReader.readLine()) != null) {
stringBuffer.append(star);
}
result = stringBuffer.toString();
inRead.close();
file.close();
} catch (Exception e) {
Log.e("WriteFile", "--- Error on reading file: "+e.toString());
}
} else {
// get the user's star or whatever
result = editRating.getText().toString();
}
Log.v("WriteFile", "--- Read file returns: " + result);
stars = Integer.parseInt(result);
totalStars = stars + 50;
try {
FileOutputStreamfileOut= openFileOutput("stars.txt", MODE_PRIVATE);
OutputStreamWriteroutputWriter=newOutputStreamWriter(fileOut);
outputWriter.write(String.valueOf(totalStars));
outputWriter.close();
fileOut.close();
// display file saved message
Toast.makeText(getBaseContext(), "File saved", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e(" WriteFile", e.toString());
}
}
At this point, when you returned to the previous activity, you should be able to see the changes.
However, in order to see the file in your storage, you unfortunately must to have a rooted device, else you'll see an empty folder. Then finally, you'd avoid to restart the Activity. You should finish the editing one, this will come back to the previous one, and you just have to call readFile()
in onResume()
instead of onCreate()
. It will update the new content into the TextView
.
Solution 2:
Have you tried on updating textview before starting new Activity? Example:
txt_start.setText(""+totalStars);
Post a Comment for "How To Read Data From Text File Then Overwrite It With New Data?"