0

Another question doesn`t respond issue with object generated out of XML out of external system, where there is not a possibility to add quotes, backslashes or any other text.

I have got script:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

public class test {
    public static void main(String[] args){

      try {
        File fileDir = new File("c:\\java2\\test.csv");

        Writer out = new BufferedWriter(new OutputStreamWriter(
            new FileOutputStream(fileDir), "UTF8"));
            out.append('\ufeff');
        out.append("Webňite, UTF-8").append(",");


        out.append("?? UTF-8");
        out.append(",");
        out.append("??????? UTF-8").append("\r\n");

        out.flush();
        out.close();

        } 
       catch (UnsupportedEncodingException e) 
       {
        System.out.println(e.getMessage());
       } 
       catch (IOException e) 
       {
        System.out.println(e.getMessage());
        }
       catch (Exception e)
       {
        System.out.println(e.getMessage());
       } 
    }   
}

I would need text "Webňite, UTF-8" to be written in one cell when converted into csv.

Is it possible please?

Currently out of 3 texts I got 4 cells in csv, and I would need 3.

I would need text which consist comma sign, to not be splitted by csv file into 2 cells.

(example script only for purpose of demonstration, at the end I have java object from XML which consist comma in it`s value and unwantedly is divided into 2 cells.

So I would need solution which would also work not for pure text within append but also for object.

Thank you

bennes
  • 61
  • 3
  • 10
  • Possible duplicate of [Is there a way to include commas in CSV columns without breaking the formatting?](https://stackoverflow.com/questions/4617935/is-there-a-way-to-include-commas-in-csv-columns-without-breaking-the-formatting) – Michael Gantman Feb 07 '19 at 13:09
  • Possible duplicate of [Dealing with commas in a CSV file](https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – baudsp Feb 07 '19 at 14:29

2 Answers2

0

Try enclosing your comma symbol in quotes. So change your line:

out.append("Webňite, UTF-8").append(",");

to

out.append("Webňite\",\" UTF-8").append(",");

See the answer Is there a way to include commas in CSV columns without breaking the formatting? (BTW this may be a duplicate question)

Michael Gantman
  • 5,975
  • 1
  • 17
  • 34
  • You should enclose the whole field – Bruce Martin Feb 07 '19 at 13:10
  • Yes, you may do so it will work as well, but you don't have to. enclosing just commas will work as well. So if you have many commas it makes sense to enclose the entire value if it is just one you can do either way – Michael Gantman Feb 07 '19 at 13:18
0

Use a Quote e.g.

out.append("\"Webňite, UTF-8\"").append(",");

or

out.append("'Webňite, UTF-8'").append(",");

or better yet use an existing CSV library e.g. super CSV etc

Bruce Martin
  • 10,213
  • 1
  • 26
  • 37
  • well the problem is, I cannot insert anything within append (). Inside append() there is an object generated from XML, which has comma inside. Is there any way how to not split it? – bennes Feb 07 '19 at 13:40