Hi, Try reading this http://developer.android.com/training/basics/data-storage/files.html and this http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int) in particular use -
- MODE_APPEND <http://developer.android.com/reference/android/content/Context.html#MODE_APPEND> - so you don't overwrite previous strings. Also your "logs" file will *not* be located in src/main/assets folder it will typically be in http://developer.android.com/reference/android/content/Context.html#getFilesDir() Regards On Friday, March 18, 2016 at 2:00:16 AM UTC+11, Jordan Ong wrote: > > 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 android-developers+unsubscr...@googlegroups.com. To post to this group, send email to android-developers@googlegroups.com. 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/b5301764-321d-423a-b7e1-38d3a76e10a1%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.