最近用Java写了一个网页数据采集的程序, 偶尔发现出现少量中文乱码的现象. 后来才知道对于中文要用字符流来进行操作.
以下是采用字节流的代码:/**
* Get the target URL's source
* Use byte stream
*
* @param url
* target URL
* @return String: source
* @throws Exception
*/
publicstaticString getUrlStr1(String url) throwsException {
InputStream is = null;
String strData = "";
try{
URL u = newURL(url); // Create URL
is = u.openStream(); // Open the URL stream
// Load the byte to the strData
byte[] myByte = newbyte[1024* 4];
intlen = 0;
while((len = is.read(myByte)) > 0) {
String st = newString(myByte, 0, len);
strData += st;
}
} catch(Exception e) {
throwe;
} finally{
is.close();
}
returnstrData;
}
下面是改进后的字符流代码:/**
* Get the target URL's source
* Use character stream
*
* @param url
* target URL
* @return String: source
* @throws Exception
*/
publicstaticString getUrlStr(String url) throwsException {
InputStream is = null;
OutputStream os = newByteArrayOutputStream();
try{
URL u = newURL(url); // Create URL
is = u.openStream(); // Open the URL stream
// Load the byte to the strData
byte[] myByte = newbyte[1024* 4];
intlen = 0;
while((len = is.read(myByte)) > 0) {
os.write(myByte, 0, len);
}
} catch(Exception e) {
throwe;
} finally{
is.close();
os.close();
}
returnos.toString();
}
通过对比发现,由于在字节流时提前转换为字符串, 如果字节数组最后只存了中文字符的前半部分, 这相当于把一个中文字符撕裂成两半, 转成String类型后就出现乱码, 而且无法逆转...