2

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();
7
  • Why do you not use a delimiter that is not appearing in your data? Commented Sep 11, 2015 at 5:56
  • We can use. But that is comment column.. might be any character they will use. If any way to remove that then that is better Commented Sep 11, 2015 at 6:00
  • If the comma is not important you can always replace it using string.replace(',', ' '). But I think you should rather use an delimiter like $$$$$ Commented Sep 11, 2015 at 6:03
  • Did you try to call writer.setUseTextQualifier(true) ? Commented Sep 11, 2015 at 6:03
  • I am using opencsv library Commented Sep 11, 2015 at 6:11

2 Answers 2

1

If you are using OpenCsv this should not be a problem: it will automatically quote values as necessary, i.e. 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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+"\"";
        }

1 Comment

The library will automatically do this if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.