如何使用Java读取Web页面

 
import static java.lang.System.out;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;


public class HtmlHelper {
	
	private static String urlstring = "http://www.csdn.net";
	private static String filename = "C:/tmp/csdn.html";
	
	public static void main(String[] args){
 
		//comment out the following section to add proxy server support
   		//System.setProperty("http.proxyHost",conf.proxyhost);
		//System.setProperty("http.proxyPort",conf.proxyport);

		InputStream is = null;
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			out.println("Downloading html page from " + urlstring);
			URL url = new URL(urlstring);
			is = url.openStream(); // throws an IOException
			br = new BufferedReader(new InputStreamReader(is));
			bw = new BufferedWriter(new FileWriter(filename));
			String line = null;
			while ((line = br.readLine()) != null) {
				bw.write(line);
				bw.newLine();
			}
			bw.flush();
			out.println("Downloaded to " + filename);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		} finally {
			try {if (is != null) {is.close();}} catch (IOException ignore) {}
			try {if (br != null) {br.close();}} catch (IOException ignore) {}
			try {if (bw != null) {bw.close();}} catch (IOException ignore) {}
		}
	}
}

你可能感兴趣的:(JAVA)