gpt4 book ai didi

java - CSV在java中显示日语hankaku(半角)而不是zenkaku(全角)

转载 作者:行者123 更新时间:2023-12-02 05:33:34 25 4
gpt4 key购买 nike

我正在使用 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

请帮我解决这个问题..

最佳答案

对我来说效果很好。您的系统可能不会默认输出为 UTF-8。当您使用 OutputStreamWriter 时,您应该始终指定。将其更改为如下所示:

bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8"));

关于java - CSV在java中显示日语hankaku(半角)而不是zenkaku(全角),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25277337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com