Java String默认是采用Unicode编码的,如何转换成别的编码格式呢。
我们考虑两种情形
1.直接对字符串操作得到指定编码格式的串
String someStr = XX;// "I Love This World!是吗?OK";
//使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中
byte[] strbytes = someStr.getBytes(
"GB2312");
String gbStr = new String(strbytes);
System.out.println( "gbStr="+gbStr);
String gbStr = new String(strbytes);
System.out.println( "gbStr="+gbStr);
这种读取只能针对someStr原本就是GB2312编码的字符串,否则可能显示乱码。
2.将某种字符编码转换成另一种编码
这里用到OutputStreamWriter, InputStreamReader. 它们可以设定字符流的编码格式。
OutputStreamWriter(OutputStream out, String charsetName)
InputStreamReader(InputStream in, String charsetName)
该类分别是FileWriter和FileReader的父类。
用ByteArrayOutputStream作为输出流,其数据会被写入一个内置的byte[]缓冲区中。
ByteArrayOutputStream()
ByteArrayInputStream(byte[] buf)
测试如下
ByteArrayOutputStream bos =
new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(bos, "GB2312");
try {
osw.append( "Hello,你好, you should cherish your time.");
osw.flush();
osw.close();
} catch (IOException ex) {
Logger.getLogger(TestStringEncoding. class.getName()).log(Level.SEVERE, null, ex);
}
//if source is encoded in "GB2312"
BufferedReader br = new BufferedReader( new InputStreamReader( new ByteArrayInputStream(bos.toByteArray()), "GB2312"));
try {
System.out.println( "line="+br.readLine());
} catch (IOException ex) {
Logger.getLogger(TestStringEncoding. class.getName()).log(Level.SEVERE, null, ex);
}
OutputStreamWriter osw = new OutputStreamWriter(bos, "GB2312");
try {
osw.append( "Hello,你好, you should cherish your time.");
osw.flush();
osw.close();
} catch (IOException ex) {
Logger.getLogger(TestStringEncoding. class.getName()).log(Level.SEVERE, null, ex);
}
//if source is encoded in "GB2312"
BufferedReader br = new BufferedReader( new InputStreamReader( new ByteArrayInputStream(bos.toByteArray()), "GB2312"));
try {
System.out.println( "line="+br.readLine());
} catch (IOException ex) {
Logger.getLogger(TestStringEncoding. class.getName()).log(Level.SEVERE, null, ex);
}
转载于:https://blog.51cto.com/dongdong1314/317971