java,
分享于 点击 9788 次 点评:162
java,
今天在用ByteArrayOutputStream导出csv时遇到了中文乱码的问题,记录下来方便自己记忆,也希望对别人有帮助。
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CSVWriter writer = new CSVWriter(outputStream);
......
byte[] fileBytes = outputStream.toByteArray(); // 这里返回了一个byte[]
FileOutputStream fos = new FileOutputStream(“xxx.csv”);
fos.write(fileBytes);
结果出现乱码,折腾了半天发现返回的数据没问题,问题在于用excel打开csv时编码的问题,我设置了UTF-8等都不可以最后发现这样可以:
FileOutputStream fos = new FileOutputStream(“xxx.csv”);
byte[] bom = {(byte)0xEF, (byte)0xBB, (byte)0xBF};// 意思也就是UTF-8
fos.write(bom);
fos.write(fileBytes);
问题解决
相关文章
- 暂无相关文章
用户点评