如何在 Java 中添加 UTF-8 BOM? [英] How to add a UTF-8 BOM in Java?

查看:67
本文介绍了如何在 Java 中添加 UTF-8 BOM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Java 存储过程,它使用 Resultset 对象从表中获取记录并创建一个 CS Vfile.

I have a Java stored procedure which fetches record from the table using Resultset object and creates a CS Vfile.

BLOB retBLOB = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION);
retBLOB.open(BLOB.MODE_READWRITE);
OutputStream bOut = retBLOB.setBinaryStream(0L);

ZipOutputStream zipOut = new ZipOutputStream(bOut);
PrintStream out = new PrintStream(zipOut,false,"UTF-8");
out.write('ufeff');
out.flush();

zipOut.putNextEntry(new ZipEntry("filename.csv"));
while (rs.next()){
    out.print(""" + rs.getString(i) + """);
    out.print(",");
}
out.flush();

zipOut.closeEntry();
zipOut.close();
retBLOB.close();

return retBLOB;

但是生成的 CSV 文件没有显示正确的德语字符.Oracle 数据库还有一个 NLS_CHARACTERSET UTF8 值.

But the generated CSV file doesn't show the correct German character. Oracle database also has a NLS_CHARACTERSET value of UTF8.

请提出建议.

推荐答案

要以 UTF-8 编写 BOM,您需要 PrintStream.print(),而不是 PrintStream.write().

To write a BOM in UTF-8 you need PrintStream.print(), not PrintStream.write().

此外,如果您想在 csv 文件中包含 BOM,我想您需要在 putNextEntry() 之后打印 BOM.

Also if you want to have BOM in your csv file, I guess you need to print a BOM after putNextEntry().

这篇关于如何在 Java 中添加 UTF-8 BOM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆