下面的方法是把网页保存到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乱码

解决方案 »

  1.   

    java 默认utf-8,试试抓一个utf8的网页看看
      

  2.   

    可以指定编码
    new FileOutputStream("out.txt"),"GB2312")
      

  3.   

    设置输出时的编码,如果没有设置的话,就默认使用你系统的编码了
    因为你系统的编码与网页内容的编码不一致,所以输出乱码BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("out.txt"),"GBK"));
      

  4.   

    已经实现了,在页面中加入<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 就可以了