[Java] Create CSV files with BOM

public static final String UTF8_BOM = "\uFEFF";

public ByteArrayInputStream createFile(Object object) {
    try {
        CSV csv = (CSV) object;
        StringBuffer sb = new StringBuffer();
        sb.append(UTF8_BOM);

        if (csv.getHeaderList() != null) {
            sb.append(StringUtils.join(csv.getHeaderList().toArray(new String[0]), ",")).append("\n");
        }

        for (List<String> row : csv.getDataList()) {
            sb.append(StringUtils.join(row.toArray(new String[0]), ",")).append("\n");
        }

        return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
    } catch (Exception e) {
        return null;
    }
}
This entry was posted in Java. Bookmark the permalink.