/**
* 改变CSV文件的编码 由gbk改为utf8
* @author xuchangcheng
* 2019年9月5日
*
*/
public class ConversionCSVcoding {
private final static String SOURCE_ENCODING = "GBK";
private final static String TARGET_ENCODING = "UTF-8";
private static String SOURCE = "E:/360/report/chuchong/11.csv";
private static String TARGET = "E:/360/report/chuchong/1111.csv";
private static void exchange(String sourcePath, String targetPath) throws IOException {
FileInputStream fin = null;
FileOutputStream fout = null;
FileChannel fcin = null;
FileChannel fcout = null;
File source = new File(sourcePath);
File target = new File(targetPath);
try {
fin = new FileInputStream(source);
fout = new FileOutputStream(target);
fcin = fin.getChannel();
fcout = fout.getChannel();
ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
while (true) {
buffer.clear();
int r = fcin.read(buffer);
if (r == -1) {
break;
}
buffer.flip();
String encoding = SOURCE_ENCODING;
fcout.write(ByteBuffer.wrap(Charset.forName(encoding).decode(buffer).toString().getBytes(TARGET_ENCODING)));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
fin.close();
}
if (fcin != null) {
fcin.close();
}
if (fout != null)
fout.close();
if (fcout != null)
fcout.close();
}
}
public static void main(String[] args) {
try {
exchange(SOURCE, TARGET);
} catch (Exception e) {
e.printStackTrace();
}
}
}