• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Writing String to Text file.

IatroJordan

Lurker
Hi, I am trying to write a string which is the output of my counter into a text file. I tried using the android coding from the tutorial but it still doesn't work. Can anyone advice me on this? My text file is inside the directory src/main/assets.

  1. public void saveText(){
    int seconds;
    FileOutputStream outputStream;
    String saveText;
    long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();
    seconds = (int) (elapsedMillis / 1000);
    try {
    outputStream = openFileOutput("logs.txt", MODE_APPEND);
    saveText = String.valueOf(seconds);
    outputStream.write(saveText.getBytes());
    outputStream.close();
    Toast.makeText(this, saveText, Toast.LENGTH_LONG).show();
    } catch (Throwable t){
    Toast.makeText(this, "Exception: " + t.toString(), Toast.LENGTH_LONG).show();
    }
    }
 
Sorry, string was not written to the text file. I check the string by using a toast and the string display correctly. I tried writing a string straight into the logs file but it does not work as well.
 
Assuming you did not get any exceptions, the data has been written to the file.

Questions:
1. Where do you think this file is being created?
2. How are you checking that the file exists?
 
Back
Top Bottom