您的位置 电脑知识爱好者 >> 编程知识 >> 这样下载的网页为什么在记事本中显示和控制台上不一样???

这样下载的网页为什么在记事本中显示和控制台上不一样???

电脑知识爱好者互联网2007-6-19 11:06:17
知识重点:我利用以下程序下载一个网页: import java.io.*; import java.net.URL; import javax.swing.*; //只下载每页的内容 public class ReadSina { public static void main(String[] args) thr..

我利用以下程序下载一个网页:
import java.io.*;
import java.net.URL;
import javax.swing.*;

//只下载每页的内容
public class ReadSina {

public static void main(String[] args) throws IOException{

String shuru;
int b;
String str="";
shuru=JOptionPane.showInputDialog("Input an URL:");

FileWriter fw=new FileWriter("D://hanhan.txt",true);

URL url = new URL(shuru); //读取URL
InputStream is = url.openStream();
BufferedInputStream bis = new BufferedInputStream(is); //封装成缓存,提高效率

while((b=bis.read())!=-1)
{str = str+(char)b;}
is.close();
bis.close();

try{
str = new String(str.getBytes("ISO-8859-1"),"UTF-8"); //以ISO-8859-1读取,以GB2312存起来
}catch(Exception e){System.out.println ("sorry");}

fw.write(str);

System.out.println (str);

}
}


程序运行时输入:http://blog.sina.com.cn/twocold
结果是在控制台上显示的内容和积存在D盘上的内容不一样,主要是格式乱了,请问高手应该怎样解决???

回答列表

参与评论
相关内容
关于我们 | 隐私政策 | 站点地图 | 站长博客|京ICP备07025396号
添加到百度搜藏 添加到百度搜藏 电脑知识爱好者Copyright ?2006-2008版权所有 我要啦免费统计