1. Are you ready for the Galaxy S20? Here is everything we know so far!

Writing String to Text file.

Discussion in 'Android Development' started by IatroJordan, Mar 23, 2016.

  1. IatroJordan

    IatroJordan Lurker
    Thread Starter

    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();
      }
      }
     


  2. IatroJordan

    IatroJordan Lurker
    Thread Starter

    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.
     
  3. Deleted User

    Deleted User Guest

    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?
     
Loading...
Similar Threads - Writing String Text
  1. Musician888
    Replies:
    0
    Views:
    278
  2. FoxMulder
    Replies:
    0
    Views:
    411
  3. akkha
    Replies:
    2
    Views:
    603
  4. restore reset reinstall
    Replies:
    1
    Views:
    265
  5. livingneanderthal
    Replies:
    5
    Views:
    990
  6. Utkarsh4
    Replies:
    0
    Views:
    960
  7. stoned frog
    Replies:
    2
    Views:
    863
  8. Soumyaranjan Pal
    Replies:
    3
    Views:
    441
  9. Mercede
    Replies:
    0
    Views:
    490
  10. ehyoussef
    Replies:
    1
    Views:
    1,102

Share This Page

Loading...