Search code examples
javaandroidopencsv

Is removing separator when exporting data with openCSV possible?


In my app i export data from database table to .csv file with opencsv. It works nicely. BUT customer need those written lines without the separator comma. Is this possible?

I've thought and tried changing separator with CSVParserWriter.DEFAULT_SEPARATOR = ''; but char iterator cannot be empty.

Below is my exportData method

private void exportData() {

        DatabaseHelper dbhelper = new DatabaseHelper(getApplicationContext());
        File exportDir = new File(Environment.getExternalStorageDirectory(), "kerailymestari_tiedostot");
        if (!exportDir.exists())
        {
            exportDir.mkdirs();
        }

        File file = new File(exportDir, kerailyNimi+".csv");
        try
        {
            file.createNewFile();
            CSVWriter csvWrite = new CSVWriter(new FileWriter(file), CSVParserWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER, CSVWriter.DEFAULT_LINE_END);
            SQLiteDatabase db = dbhelper.getReadableDatabase();
            Cursor curCSV = db.rawQuery("SELECT * FROM tuotteet WHERE KID = " + kid,null);
            //csvWrite.writeNext(curCSV.getColumnNames());
            while(curCSV.moveToNext())
            {
                //Which column you want to exprort
                String arrStr[] ={curCSV.getString(1),curCSV.getString(2), curCSV.getString(3), curCSV.getString(4)};
                csvWrite.writeNext(arrStr);
            }
            csvWrite.close();
            curCSV.close();
        }
        catch(Exception sqlEx)
        {
            Log.e("Results", sqlEx.getMessage(), sqlEx);
        }
    }

That code needs to be changed so that separator would be remove or changed. Or are there better solutions?


Solution

  • I don't see why you'd keep using openCsv then. But if you want to do it further, just concat the strings from the columns on each other and create an array with the length 1. Something like below should work:

    String arrStr[] ={curCSV.getString(1) + curCSV.getString(2) + curCSV.getString(3) +  curCSV.getString(4)};
                csvWrite.writeNext(arrStr);
    

    Note the concatenation using + operator instead of your original ,.