- Forum posts: 2
Mar 17, 2016, 4:24:46 PM via Website
Mar 17, 2016 4:24:46 PM via Website
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>
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.