'how to export a text file with PadRight space android

I want to ask export data to text file with spaces(PadRight) like this

Date (max 10 characters) and Barcode (max 14 characters) and Qty 1 (max 8 characters) and Qty 2 (max 8 characters)

Date       Barcode        Q1       Q2
--------------------------------------
21/03/2022,123456        ,10      ,4
21/03/2022,0909          ,3       ,9

now i don't use spaces(pad) like this

Date Barcode Q1 Q2

21/03/2022,123456,10,4

21/03/2022,0909,3,9

This is my code

  btn_export.setOnClickListener(new View.OnClickListener() {
        SQLiteDatabase db = controller.getReadableDatabase();
        Cursor c = null;
    
        @Override
        public void onClick(View v) { //main code begins here
            try {
                c = db.rawQuery("select TanggalScan,KodeBarcode,QtyGudang,QtyToko from tblscandata", null);
                int rowcount = 0;
                int colcount = 0;
    
                File sdCardDir = Environment.getExternalStorageDirectory();
                String filename = "OPANAME.txt";

                File saveFile = new File(sdCardDir,filename);
                FileWriter fw = new FileWriter(saveFile);
                Log.e("File path", filename);
    
                BufferedWriter bw = new BufferedWriter(fw);
                rowcount = c.getCount();
                colcount = c.getColumnCount();
                if (rowcount > 0) {
                    c.moveToFirst();
    
                  
                    for (int i = 0; i < rowcount; i++) {
                        c.moveToPosition(i);
                        for (int j = 0; j < colcount; j++) {
                            if (j != colcount - 1)
                                bw.write(c.getString(j) + ",");
                            else
                                bw.write(c.getString(j));
                        }
                        bw.newLine();
                    }
                    bw.flush();
                    lbl.setText("Exported Successfully.");
    
                    controller = new DBController(getApplicationContext());
                    SQLiteDatabase db = controller.getWritableDatabase();
    
                    db.execSQL("delete from " + DBController.TableScan);
    
                }
            } catch (Exception ex) {
                if (db.isOpen()) {
                    db.close();
                    lbl.setText(ex.getMessage().toString());
                }
    
            } finally {
    
            }
        }
    });

this my code



Sources

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

Source: Stack Overflow

Solution Source