JavaSE系列代码69:利用URL获取网上文件的内容

There are two types of threads in Java: user thread: user created thread; sprite thread: the thread running in the background and serving other threads; the garbage collector is sprite thread; when the JVM detects that there are only sprite threads, it will exit.

import java.net.*;
import java.io.*;
public class Javase_69
{
  public static void main(String[] args)
  {
    String urlname = "http://www.edu.cn/index.html";
    if (args.length>0)   urlname=args[0];
    new Javase_69().display(urlname);
  }
  public void display(String urlname)
  {
    try
    {
      URL url=new URL(urlname);     //创建URL类对象url
      InputStreamReader in=new InputStreamReader(url.openStream());
      BufferedReader br=new BufferedReader(in);
      String aLine;
      while((aLine=br.readLine())!=null)   //从流中读取一行显示
        System.out.println(aLine);
    }
    catch(MalformedURLException murle)
    {  System.out.println(murle); }
    catch(IOException ioe)
    {  System.out.println(ioe);  }
  }
}

发布了73 篇原创文章 · 获赞 189 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/blog_programb/article/details/105569113