java 数据库写入,指定GBK 编码

本身生成文件时ANSI,写入数据后就变成UTF-8,OutputStreamWriter osw = new OutputStreamWriter(fos,"ANSI");写入数据直接抛出异常,其实在中国GBK就是ANSI,



public String WriteTXTCol(String data, File file) {

String resmsg = "success";
try {
// 分隔字符串
String[] splitStr = null;
splitStr = data.split("\\|"); // 转义字符,必须加“\\”
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
// 遍历数组,将数组存入ftp指定的TXT文件中
for (int i = 0; i < splitStr.length; i++) {
String str = splitStr[i];
str += "\r\n";
System.out.println(splitStr[i]);
osw.write(str);


}
osw.close();
} catch (Exception e) {
resmsg = "error:写入TXT文件失败!";
}
return resmsg;

}


你可能感兴趣的:(Java编程-数据写入)