常见编码和编码头BOM:https://www.cnblogs.com/signheart/p/c3b1000186199e89d4e02c33f39ed418.html
乱码问题很头疼,日文的乱码问题更是头疼。按照常理来讲日本人较真工匠精神那么按理来说搜索工具类对应的日文应用问题应该能很好的搜索出来问题的解决方案。但是结果让人惊讶,日文应用乱码的问题搜索结果出来最多的是英文和中文,最后才是日文。打开日文的文章一看,大跌眼镜,简简单单的把最基础的测试用例一COPY算是完事了,完全对不起他们的民族精神。再回过头来说问题,日文导出CSV有两个问题需要解决:
具体的实现咱们看下代码:
package com.yneit.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.util.NumberUtils;
import org.supercsv.cellprocessor.FmtBool;
import org.supercsv.cellprocessor.FmtDate;
import org.supercsv.cellprocessor.constraint.LMinMax;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.UniqueHashCode;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvListWriter;
import org.supercsv.io.ICsvListWriter;
import org.supercsv.prefs.CsvPreference;
import au.com.bytecode.opencsv.CSVWriter;
/**
* CSV 日文乱码解决
*/
public class Test {
// 日文字使用符集
public static String UTF_16LE = "UTF-16LE";
public static String UTF_8 = "UTF-8";
public static void main(String[] args) throws IOException {
String path = "G:\\Project_Java\\MyProject\\src\\com\\yneit\\work\\test.csv";
String encoder = UTF_8;
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(path), encoder);
out.write(0xFEFF);
CSVWriter writer = new CSVWriter(out, CSVWriter.DEFAULT_SEPARATOR,
CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END);
String[] entries = { "1", "fir", "リージョン", "6", "人民", "ond,政府\"ird" };
String[] datas = new String[entries.length];
for (int i = 0; i < entries.length; i++) {
String item = Test.verityCell(entries[i]);
datas[i] = item;
}
writer.writeNext(datas);
writer.close();
System.out.println("over");
try {
Test.writeWithCsvListWriter();
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* @return the cell processors
*/
private static CellProcessor[] getProcessors() {
final CellProcessor[] processors = new CellProcessor[] { new UniqueHashCode(), // customerNo
// (must
// be
// unique)
new NotNull(), // firstName
new NotNull(), // lastName
new FmtDate("dd/MM/yyyy"), // birthDate
new NotNull(), // mailingAddress
new Optional(new FmtBool("Y", "N")), // married
new Optional(), // numberOfKids
new NotNull(), // favouriteQuote
new NotNull(), // email
new LMinMax(0L, LMinMax.MAX_LONG) // loyaltyPoints
};
return processors;
}
/**
* An example of reading using CsvListWriter.
*/
private static void writeWithCsvListWriter() throws Exception {
// create the customer Lists (CsvListWriter also accepts arrays!)
final List