简体   繁体   中英

Remove spaces when writing to CSV using CSVWriter in java

I am reading from table and writing to CSV file using CSVWriter in JAVA. My delimiter is 'control-A' . But one column is having spaces in comments. So how to remove that spaces from value while writing to CSV file. My code is

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("query");
CSVWriter writer = new CSVWriter(new FileWriter(filePath + "File_writerCSV.csv"),'\u0001');
writer.writeAll(rs,false);
writer.close();

If you are using OpenCsv this should not be a problem: it will automatically quote values as necessary, ie if one of the values you write is some,comment OpenCsv will actually write "some,comment" (enclosed in qoutes). All standard csv; nothing you have to worry about.

other way is if a value contains ',' character put that value in quotes. of example if value is BMW,AUDI,MERc --> convert it into "BMW,AUDI,MERC". so it will be considered 1 entity.

String s = "as,as,as";
        if(s.contains(","))
        {
            s="\""+s+"\"";
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM