readurl.java

来自「Java Classic Examples是我买的两本书:《JAVA经典实例》和」· Java 代码 · 共 46 行

JAVA
46
字号
import java.net.*;
import java.io.*;

class ReadURL 
{
  public static void main(String[] args)
  {
    try
    {
      // Define a URL
      URL sourceURL = new URL("http://www.ncsa.uiuc.edu/demoweb/url-primer.html");

      // Get a character input stream for the URL
      BufferedReader in = new BufferedReader(
                          new InputStreamReader(
                              sourceURL.openStream()));

      // Create the stream for the output file
      PrintWriter out = new PrintWriter( 
                        new BufferedWriter(
                        new FileWriter(
                        new File("C:\\JunkData\\netdata.html"))));

      System.out.println("Reading the file " + sourceURL.getFile() +
                         " on the host " + sourceURL.getHost() +
                         " using " + sourceURL.getProtocol());

      // Read  the URL and write it to a file
      String buffer;                          // Buffer to store lines
      while(!(null==(buffer=in.readLine())))
        out.println(buffer);

      in.close();                             // Close the input stream
      out.close();                            // Close the output file
    }
    catch(MalformedURLException e)
    {
      System.out.println("Failed to create URL:\n" + e);
    }
    catch(IOException e)
    {
      System.out.println("File error:\n" + e);
    }
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?