---
title: "java utf8字符 导出csv 文件的乱码问题。"
author: "杨俊华"
date: "2011-09-15T09:31:17Z"
tags: []
categories: ["编程语言"]
slug: "java-utf8-csv"
draft: false
lastmod: "2011-09-15T09:31:17Z"
id: 1172200
url: "https://hbase.iteye.com/blog/1172200"
---

一个同事遇到utf-8 导出到csv文件时出现乱码。
上网查了一下，需要一个BOM头。MS的东西就是麻烦。
加上头，问题解决。
现在把参考代码贴出来，也留个记录。
这只是在同事测试代码上面改的。

所以写起来比较随意。见谅。



```java

      private static void writeBcp( ) throws IOException
      {
            //Create bcp file if not exist
                        File bcpFile = new File("test.csv");
                        //bcpFile.delete();
                        byte[] bom ={(byte) 0xEF,(byte) 0xBB,(byte) 0xBF};
                        //boolean newFile = false;
                        FileOutputStream bcpFileWriter = new FileOutputStream(bcpFile);
                        bcpFileWriter.write(bom);
                  //bcpFile.delete();
                        String title = "\"MD5\",\"扫描文件名\",\"扫描时间\"," +
                        "\"是否病毒\",\"安全等级\",\"病毒英文名称\"," +
                        "\"病毒变种\",\"病毒类型\",\"病毒影响\"," +
                        "\"感染系统\",\"传播方式\",\"备注\"";
                        
                        
                        bcpFileWriter.write((new String(title.getBytes(), "utf-8")).getBytes());
                        bcpFileWriter.write("\n".getBytes());
                        
                        String appStr = "\""+123+"\","
                        +"\""+123+"\","
                        +123+","
                        +123+","
                        +123+","
                        +"\""+123+"\","
                        +"\"\","
                        +123+","
                        +"\""+123+"\","
                        +"\""+123+"\","
                        +"\""+123+"\","
                        +"\""+123+"\"\n";
                        

                        bcpFileWriter.write(appStr.getBytes());
                        bcpFileWriter.close();
                        
      }


```