<span style="font-size:14px;">package test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
//读取网页内容URLReader
public class URLReader {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 声明抛出所有例外
URL url = new URL("http://www.sina.com");// 待读取的网页
File writeFile = new File("D:\\AA\\sina.html");// 读取的首页内容存储到本地路径
// 之前乱码但是发现sina首页
// <meta http-equiv="Content-type"content="text/html; charset=gb2312" />
// 所以使用InputStreamReader字节转为字符的时候,使用gb2312编码不乱码
BufferedReader in = new BufferedReader(new InputStreamReader(
url.openStream(), "gb2312"));
// 使用OutputStreamWriter字节转为字符的写文件的时候,使用gb2312编码不乱码
BufferedWriter bos = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(writeFile), "gb2312"));
// OutputStreamWriter bos1 =new OutputStreamWriter(new
// FileOutputStream(writeFile),"gb2312");
// BufferedWriter bos1 = new BufferedWriter(new FileWriter(writeFile));
String inputLine;
// 使用openStream得到一输入流并由此构造一个BufferedReader对象
while ((inputLine = in.readLine()) != null) {
bos.write(inputLine);
}
bos.flush();
in.close();
bos.close();
}
}
</span>
先把这段代码留存,记录一下
读取网页内容
最新推荐文章于 2024-05-17 08:30:00 发布