I am new to android programming and I am trying to write strings into a 
text file without overwriting the previous one. It is somehow like a 
history log of the application. I tried reading online guide but it doesnt 
seem to write in to my text file. Can anyone advice me on this? My empty 
text file name logs is inside src/main/assets folder. Everytime the 
saveText function is called, it successfully prompt the Toast that shows 
the string of the saveText, so I wonder why it did not write into the logs 
textfile.

public void saveText(){
    int seconds;
    FileOutputStream outputStream;
    String saveText;
    long elapsedMillis = SystemClock.elapsedRealtime() - mChronometer.getBase();
    seconds = (int) (elapsedMillis / 1000);
    try {
        outputStream = openFileOutput("logs", Context.MODE_PRIVATE);
        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();
    }}

Android Manifest:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".GameActivity" />
    <activity android:name=".EndActivity" /></application>

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/9d9d3e20-7326-41b8-89c6-ac046f54e286%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to