下面的方法是把网页保存到SD卡一个文件中,打开时没有乱码,打印时却有乱码。
//将页面保存到本地文件夹
public void doWrite(String url_str)
{
URL url = null;
FileOutputStream fos = null;
InputStream is;
try {
for (int i = 0; i < 1; i++) {
url = new URL(url_str);
ByteArrayOutputStream data=new ByteArrayOutputStream();
byte bytes[] = new byte[1024 * 2000];
int index = 0;
is = url.openStream();
int count = is.read(bytes, index, 1024 * 2000);
while (count != -1) {
index += count;
count = is.read(bytes, index, 1);
}
url1=System.currentTimeMillis();//保存名设置
fos = new FileOutputStream("sdcard/dyhtml/"+url1+".html");
fos.write(bytes, 0, index);
is.close();
fos.close();
}
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Java 乱码
------解决方案--------------------
java 默认utf-8,试试抓一个utf8的网页看看
------解决方案--------------------
可以指定编码
new FileOutputStream("out.txt"),"GB2312")------解决方案--------------------