JavaSE之URL编程实现

使用URL从网上获取资源:

public class Test {
	public static void main(String[] args) throws IOException {
		URL url = new URL("https://www.csdn.net/");
		//打开此URL的链接
		InputStream is=url.openStream();
		//将字节输入流转换为字符输入流
		InputStreamReader isr = new InputStreamReader(is);
		//将字节输出流转换为字符输出流,并将链接内容写入1.html文件
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\1.html"));
		BufferedReader br = new BufferedReader(isr);
		BufferedWriter bw = new BufferedWriter(osw);
		char[] cuf=new char[1024];
		int line;
		while((line=br.read(cuf))!=-1)
		{
			bw.write(cuf, 0, line);//写入1.html
			System.out.println(new String(cuf,0,line));//输出链接内容
		}
		br.close();
		bw.close();
	}
}

你可能感兴趣的:(JavaSE之URL编程实现)