Search code examples
javacsvutf-8bytearrayinputstream

Export csv with UTF-8 encoding


I want to ensure that my written CSV-file is encoded using UTF-8:

public StreamResource getExportDataAsCsv() {
    return new StreamResource(new StreamResource.StreamSource() {
        @Override
        public InputStream getStream() {
            List<Object[]> exportData = getExportData();
            StringBuffer buffer = new StringBuffer();

            buffer.append(someData);
            byte[] bytes = buffer.toString().getBytes();

            return new ByteArrayInputStream(bytes);
        }
    }, "MyExport.csv");
}

Unfortunately the found methods/ways to do so don't fit to my interface. Any ideas?


Solution

  • buffer.toString().getBytes("UTF-8");