Java读取网络上的文件内容

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class ReadHtmlFile {
    public static void main(String[] args) {
        String sourcePath="http://www.sina.com/robots.txt";//读取新浪网站的机器人向导
        new ReadHtmlFile().readHtmlFile(sourcePath);
    }

    /**
     * 读取文件
     * @param sourcePath 文件所在的网络路径
     */
    public void readHtmlFile(String sourcePath){
        String line;
        int lineNum=0;
        BufferedReader reader=null;
        try{
            URL url = new URL(sourcePath);
            reader = new BufferedReader(new InputStreamReader(url.openStream()));
            while ((line = reader.readLine()) != null){
                lineNum++;
                System.out.println(line);
            }
        }
        catch (Exception ie){
            ie.printStackTrace();
        }finally{
            try{
                if(reader != null)
                    reader.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(java,.net,IE)