Writing strings to text file android

  • Replies:6
  • OpenNot stickiedUnanswered
  • Forum posts: 2

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);
        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"/>

    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
    <activity android:name=".GameActivity" />
    <activity android:name=".EndActivity" />
  • Forum posts: 256

Mar 17, 2016 7:34:17 PM via Website

Hi, your file named "logs" maybe is there: /data/data/your_package_name/files/

— modified on Mar 17, 2016 7:36:05 PM

  • Forum posts: 207

Mar 18, 2016 6:42:46 AM via Website

public static void writeStringToTextFile(String s, String f) {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath()+"/MMDLogs");
File file = new File(dir, f);
catch(Exception e){
Log.d("file", e.getMessage());

    try {
        FileOutputStream f1 = new FileOutputStream(file, true); // True =
                                                                    // Append
                                                                    // to
                                                                    // file,
                                                                    // false
                                                                    // =
        //f1.write(s.getBytes());                                                           // Overwrite
        PrintStream p = new PrintStream(f1);
        Log.d("file string", s);
        Log.d("file", "file created");
    } catch (FileNotFoundException e) {
        Log.d("file", "file not found");
    } catch (IOException e) {
        Log.d("file", "ioexception");
    }catch (Exception e){
        Log.d("file", "exception");
  • Forum posts: 14

Mar 18, 2016 7:35:12 AM via Website

hi are you very good in codes for andriod studio ?

  • Forum posts: 14

Mar 18, 2016 8:30:39 AM via Website

can you email me at wenhan999@hotmail.com

  • Forum posts: 9

Mar 18, 2016 12:34:21 PM via Website

I have altered the code as described (xml addition) but I still get a write error on writing the file. I have added the mount check and also placed a toast message which shows de path of the SD card on my test device /storage/sd card so I'm pretty shore it's mounted. Then I looked again and saw I placed the lines in the manifest within the application tags... they have to be placed outside. This changed things for the better... thanks for the fine example.