原本写这段读/写主要是解决MAC和Win2000的编码问题 .
读:
FileInputStream fileRead = new FileInputStream(fileName);
InputStreamReader inStreamR = new InputStreamReader(fileRead, "gb2312");
BufferedReader buffer = new BufferedReader(inStreamR);写:
FileOutputStream fileWriter = new FileOutputStream(fileName);
OutputStreamWriter outStreamWriter = new OutputStreamWriter(fileWriter, "gb2312");
PrintWriter print = new PrintWriter(outStreamWriter);编码转化:
public static String UTF8toGB2312(String str){
if( null == str ){
return "";
}
try{
byte[] strTemp = str.getBytes();
str = new String(str.getBytes("UTF-8"), "ISO-8859-1");
return str;
}catch(Exception e){
Debug.out("String UTF8toISO88591 error: " + e.toString());
return "";
}
} public static String GB2312toUTF8(String str){
if( null == str ){
return "";
}
try{
byte[] strTemp = str.getBytes();
str = new String(str.getBytes("gb2312"), "UTF-8");
return str;
}catch(Exception e){
Debug.out("String GB2312toUTF8 error: " + e.toString());
return "";
}
}
读:
FileInputStream fileRead = new FileInputStream(fileName);
InputStreamReader inStreamR = new InputStreamReader(fileRead, "gb2312");
BufferedReader buffer = new BufferedReader(inStreamR);写:
FileOutputStream fileWriter = new FileOutputStream(fileName);
OutputStreamWriter outStreamWriter = new OutputStreamWriter(fileWriter, "gb2312");
PrintWriter print = new PrintWriter(outStreamWriter);编码转化:
public static String UTF8toGB2312(String str){
if( null == str ){
return "";
}
try{
byte[] strTemp = str.getBytes();
str = new String(str.getBytes("UTF-8"), "ISO-8859-1");
return str;
}catch(Exception e){
Debug.out("String UTF8toISO88591 error: " + e.toString());
return "";
}
} public static String GB2312toUTF8(String str){
if( null == str ){
return "";
}
try{
byte[] strTemp = str.getBytes();
str = new String(str.getBytes("gb2312"), "UTF-8");
return str;
}catch(Exception e){
Debug.out("String GB2312toUTF8 error: " + e.toString());
return "";
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货