chiperortiz
chiperortiz

Reputation: 4991

Java OutputStreamWriter UTF-16 CVS wrong character on StartLine

i need to write a simple CSV file using OutputStreamWriter everything works OK but i have a problem a have in the first Header on the CSV the outer left on every line seems to ADD improperly a Character or a sequence of them in the String here is my Java Code

private final Character SEPARATOR=';'; 
private final Character LINE_FEED='\n';
public void createCSV(final String fileName)//......
{        
    try
    (final OutputStream outputStream = new FileOutputStream(fileName);
    final OutputStreamWriter writer=new OutputStreamWriter(outputStream,StandardCharsets.UTF_16);)
    {            
         final StringBuilder builder = new StringBuilder().append("Fecha").append(SEPARATOR)
        .append("NºExp").append(SEPARATOR)                       
        .append("NºFactura").append(SEPARATOR).append(LINE_FEED);            
        writer.append(builder.toString());                         
        writer.append(builder.toString());                         
        writer.flush();           
    }catch (IOException e){e.printStackTrace();}    
}

unfortunalety i am receiving this ouput always happens in the first line if i repeat the same output to the second line in the CSV everything works smoothly is a Java problem or is my Excel gives me nightmares??.. thank a lot..

OUTPUT

enter image description here

Upvotes: 0

Views: 1499

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109547

This is a superfluous byte order mark (BOM), \uFFFE, a zero width space, its byte encoding used to determine whether it is UTF-16LE (little endian) or UTF-16-BE (big endian).

Write "UTF16-LE", which has the Windows/Intel ordering of least significant byte, most significant byte.

StandardCharsets.UTF_16LE

Upvotes: 1

Related Questions