集册 Java实例教程 从url生成和读取

从url生成和读取

欢马劈雪     最近更新时间:2020-01-02 10:19:05

534
使用java.net.URL类创建URL。
/*n o w  j a v a  . c o m*/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.MalformedURLException;

import java.net.URL;


public class Main {


  public static void main(String[] args) {

    try {

      URL url1 = new URL("http://www.java.net");

      System.out.println(url1.toString());


      // Generate URL for pages with a common base

      URL url2 = new URL(url1, "search/node/jdk7");/**来 自 NowJava.com - 时  代  Java**/


      // Generate URL from different pieces of data

      URL url3 = new URL("http", "java.net", "search/node/jdk7");


      readFromUrl(url1);


    } catch (MalformedURLException ex) {

      ex.printStackTrace();

    }

  }


  public static void readFromUrl(URL url) {

    try {

      BufferedReader in = new BufferedReader(new InputStreamReader(

          url.open
展开阅读全文