1. Download our Official Android App: Forums for Android!

Apps Writing String to Text file.

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

  1. IatroJordan

    IatroJordan Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Mar 23, 2016

    Mar 23, 2016
    3
    0
    5
    Male
    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();
      }
      }
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
  3. IatroJordan

    IatroJordan Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    3
    Joined:
    Mar 23, 2016

    Mar 23, 2016
    3
    0
    5
    Male
    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.
     
  4. LV426

    LV426 I say we take off and nuke this place from orbit
    Recognized Developer
    Rank:
     #12
    Points:
    1,988
    Posts:
    9,176
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    9,176
    13,681
    1,988
    Male
    Software developer
    South West of England
    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?
     

Share This Page

Loading...