'OutputStreamWriter.append not appending text to a text file Android Programming

File file; FileOutputStream fileOutputStream;

    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);

    for (Object obj : pkgAppsList) {
        ResolveInfo resolveInfo = (ResolveInfo) obj;
        PackageInfo packageInfo = null;
        try
        {

            file = new File(Environment.getExternalStorageDirectory(),"MyResearchFile.txt");
            FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos);


            packageInfo = getPackageManager().getPackageInfo(resolveInfo.activityInfo.packageName, PackageManager.GET_PERMISSIONS);
            String[] requestedPermissions = packageInfo.requestedPermissions;
            final PackageManager pm = getApplicationContext().getPackageManager();
            ApplicationInfo ai;
            ai = pm.getApplicationInfo(resolveInfo.activityInfo.packageName,0);
            final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "unknown");

            for (int i = 0,j=0; i < requestedPermissions.length; i++,j++)
            {

                if(requestedPermissions[i].contains("READ_CONTACTS") )
                {
                    outputStreamWriter.append((applicationName + " requested " + requestedPermissions[i]).toString());
                }
                if(requestedPermissions[i].contains("WRITE_CONTACTS"))
                {
                    outputStreamWriter.append((applicationName + " requested " + requestedPermissions[i]).toString());

                }
            }
            outputStreamWriter.flush();
            outputStreamWriter.close();

Question : append function is not appending text to text file. I am working on an Android Application which writes all permission details to a text file.



Solution 1:[1]

Use the constructor with the append flag

FileOutputStream fos = new FileOutputStream(file, true);

Solution 2:[2]

Using OutputStreamWriter for Android, text can be appended adding Context.MODE_APPEND, like this:

OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("test.txt", Context.MODE_APPEND));

For example, this function:

   public void writeToFile(String data, Context context) {

        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("test.txt", Context.MODE_APPEND));
            outputStreamWriter.write(data);
            outputStreamWriter.close();
            System.out.println(data + " data appended to file");
        }
        catch (Exception e) {
            System.out.println("Error writing file");
            System.out.println(e);
        }
   

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Reimeus
Solution 2 programandoconro