Overwrite a file using OpenCSV

from the CommonsWare Community archives

At February 6, 2021, 12:45pm, AlanHaden asked:

This code (written mostly by yourself) and modified by me to save a CSV file works for new files.

                                try {
                                File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS + "/text/");
                                Date c = Calendar.getInstance().getTime();
                                SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());
                                String formattedDate = df.format(c);
                                fileName = contentsFromFile[0] + "-Test-cert-" + formattedDate + ".csv";
                                File file = new File(directory, fileName);

                                OutputStream output = getApplicationContext().getContentResolver().openOutputStream(Uri.fromFile(file));
                                CSVWriter writer = new CSVWriter(new OutputStreamWriter(output));
                                writer.writeNext(contentsFromFile);
                                writer.close();
                                System.out.println("CFF: " + Arrays.toString(contentsFromFile));
                                Toast.makeText(getApplicationContext(), "CSV Created", Toast.LENGTH_SHORT).show();
                            } catch (IOException e) {
                                e.printStackTrace();
                                Toast.makeText(getApplicationContext(), "CSV Creating Failed", Toast.LENGTH_LONG).show();
                            }

but when I try to update a file instead of creating a new one using the following code, it fails.

                                try {
                                File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS + "/text/");
                                File file = new File(directory, fileName);
                                OutputStream output = getApplicationContext().getContentResolver().openOutputStream(Uri.fromFile(file));
                                CSVWriter writer = new CSVWriter(new OutputStreamWriter(output));
                                writer.writeNext(contentsFromFile);
                                writer.close();
                                System.out.println("CFF: " + Arrays.toString(contentsFromFile));
                                Toast.makeText(getApplicationContext(), "CSV Saved", Toast.LENGTH_SHORT).show();
                            } catch (IOException e) {
                                e.printStackTrace();
                                Toast.makeText(getApplicationContext(), "CSV Saving Failed", Toast.LENGTH_LONG).show();
                            }

The error is:

W/System.err: java.io.FileNotFoundException: open failed: EEXIST (File exists)

How do I persuade it to ignore the existing file and overwrite regardless?
This was never an issue with the pre-Android 10 method but it is now.


At February 6, 2021, 1:38pm, mmurphy replied:

I suspect that you can’t, at least on Android 11. You have limited access to the filesystem for write operations.

I recommend that you use getExternalFilesDir(null) instead. As discussed in the other thread, you have access to that without permissions, including on Android 10+. And you are already using that directory elsewhere in your code now.

There are other, more complex alternatives that we can explore if needed.

You might wish to consider skimming through the storage-related chapters of Elements of Android Q and Elements of Android R (both available in your Warescription), to get a sense of what all has been changing.


At February 6, 2021, 2:11pm, AlanHaden replied:

How about deleting the original file once it’s loaded and then saving in its place?
Just thought.