我正在使用Java Bufferedwriter生成csv文件。我试图将zenkaku(全宽)数字附加到csv文件,但它显示hankaku而不是zenkaku。你能建议我怎么做吗? 以下是我的示例代码。
public class GenerateCSVProg {
private static StringBuffer sb = null;
private static BufferedWriter bw = null;
private static GenerateCSVProg writer = null;
public GenerateCSVProg(String fileName) throws Exception{
init(fileName);
}
private void init(String fileName) throws Exception{
try {
bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName)));
} catch (FileNotFoundException e) {
throw e;
}
}
/**
* @param args
*/
public static void main(String[] args) {
generateCsvFile("c:\\work\\test.csv");
}
private static void generateCsvFile(String fileName){
try{
writer = new GenerateCSVProg(fileName);
System.out.println("Entered in generateCsvFile method");
writer.append("DisplayName");
writer.append(",");
writer.append("Age");
writer.append("\n");
writer.append("MKYONG");
writer.append(",");
writer.append("26");
writer.append("\n");
writer.append("SIVA");
writer.append(",");
writer.append("29"); // zenkaku
writer.append("\n");
System.out.println("Entered in generateCsvFile method: " + writer);
//generate whatever data you want
bw.write(sb.toString());
System.out.println("Code break");
if(writer!=null){
writer.close();
}
}catch (Exception e) {
System.out.println("Exception occurred");
}
finally {
}
}
public void append(String out){
if (sb == null){
sb = new StringBuffer();
}
sb.append(out);
System.out.println("SB: " + sb);
}
public static void close() throws Exception{
if (bw != null){
bw.close();
}
}
}
DisplayName Age
MKYONG 26
SIVA 29 // Here it is displaying hankaku
请帮帮我..
答案 0 :(得分:2)
对我来说很好。您的系统可能不会默认输出为UTF-8。您应该始终指定何时使用OutputStreamWriter。将其更改为如下所示:
bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"));