-1

I am reading Chinese and Spanish data from the Database and writing into CSV file. I am using UTF-16. when I open CSV file in Excel þÿ is getting prepended. How do I avoid writing þÿ into CSV?

FileOutputStream os =   new FileOutputStream("s.csv", false)
 CSVWriter out = new CSVWriter(
     new BufferedWriter(
                        new OutputStreamWriter(os, "UTF-16"
                                )));
private void write(String[] values) throws IOException {    
        out.writeNext(values);
    }
Bid
  • 1
  • 3
  • 1
    Please [edit] the question, adding the code that writes to the CSV, and mentioning any libraries that you are using for this. – RealSkeptic Jun 13 '17 at 08:07
  • 1
    I wonder if it is clever to remove the byte order mark from UTF-16 files ... – Tom Jun 13 '17 at 08:14
  • 1
    @Tom It would not be a good idea if you want the file to be portable across systems. But I wonder why one would want to write UTF-16 in the first place. – laune Jun 13 '17 at 08:18
  • Could you try to use the tab character as a delimiter in your CSV file ? – Arnaud Jun 13 '17 at 08:20

1 Answers1

2

These two characters are the Byte Order Mark (for big endian) that can be used with the UTF-16 encoding.

To write without prepending the byte order mark, use the java.nio.Charset identified by UTF-16BE, not just UTF-16.

laune
  • 31,114
  • 3
  • 29
  • 42