'Save a database (from Android App) into a cvs file

I want to save my database to a CSV file. The whole thing runs via an Android app with the API 23. I would like to have created a folder where the files are stored. I would like to be able to access the files without another app.

It´s going to be an attendance list. Childmemo is an Object for a child. (firstname and lastname are Strings). The Log: "filewriter is working" never comes up. Everything else is working pretty well. I have these Errors:

2022-02-24 14:46:27.513 5309-5309/com.ovgu.temi_projekt W/System.err: at com.ovgu.temi_projekt.MainActivity.saveData(MainActivity.java:377) 2022-02-24 14:46:27.513 5309-5309/com.ovgu.temi_projekt W/System.err: at com.ovgu.temi_projekt.MainActivity$8.onClick(MainActivity.java:273)

Line 273 is the savedata call inside of the Click Listener. Line 377 is this Line inside of the savedata function:

FileWriter writer = new FileWriter(name + ".csv");

That´s my code:

newDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ChildMemo> childMemoList = dataSource.getAllChildMemos();
                Log.d(LOG_TAG, "Liste erstellt");
                try {
                    Log.d(LOG_TAG, "Ist im try");
                    saveData(childMemoList);
                    dataSource.deleteChildMemo();
                    Log.d(LOG_TAG, "Daten gelöscht");
                }catch(Exception e){
                    Log.d(LOG_TAG, "FileWriter konnte nicht erstellt werden");
                }



            }
        });

public void saveData(List<ChildMemo> childmemo){
        String delimiter = ",";
        String CRLF = "\r\n";
        String name = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
        try {
            FileWriter writer = new FileWriter(name + ".csv");
            Log.d(LOG_TAG, "FileWriter is working");
            for (int i = 0; i< childmemo.size(); i++){
                writer.append(childmemo.get(i).getFirstname());
                writer.append(delimiter);
                writer.append(childmemo.get(i).getLastname());
                writer.append(CRLF);
            }
            writer.flush();
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Where is my mistake or is there a better option. (iText does not work.)

Thanks for your help <3



Sources

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

Source: Stack Overflow

Solution Source