Java URI toExternalForm()方法及示例

来源:这里教程网 时间:2026-02-17 21:16:05 作者:

Java URI toExternalForm()方法及示例

toExternalForm()函数是URL类的一部分。toExternalForm()函数返回一个指定的URL的字符串表示。这个字符串是通过调用这个对象的流协议处理程序的toExternalForm()函数创建的。

函数签名

public String toExternalForm()

语法

url.toExternalForm()

参数: 此函数不需要任何参数
返回类型: 此函数返回字符串类型

下面的程序说明了toExternalForm()函数的使用。

例1 :给定一个URL,我们将对该URL的字符串进行表示

// Java program to show the// use of the function toExternalForm()  import java.net.*;  class Solution {    public static void main(String args[])    {        // url  object        URL url = null;          try {            // create a URL            url = new URL("https:// www.geeksforgeeks.org");              // get the  ExternalForm            String _ExternalForm = url.toExternalForm();              // display the URL            System.out.println("URL = " + url);              // display the  ExternalForm            System.out.println(" String representation= "                               + _ExternalForm);        }        // if any error occurs        catch (Exception e) {              // display the error            System.out.println(e);        }    }}

输出:

URL = https:// www.geeksforgeeks.org String representation= https:// www.geeksforgeeks.org

例2:

// Java program to show the// use of the function toExternalForm()  import java.net.*;  class Solution {    public static void main(String args[])    {        // url  object        URL url = null;          try {              // create a URL            url= new URL("https:// www.geeksforgeeks.org#Arnab_Kundu");                          // get the  ExternalForm            String _ExternalForm = url.toExternalForm();              // display the URL            System.out.println("URL = " + url);              // display the  ExternalForm            System.out.println(" String representation= "                               + _ExternalForm);        }          // if any error occurs        catch (Exception e) {              // display the error            System.out.println(e);        }    }}

输出:

URL = https:// www.geeksforgeeks.org#Arnab_Kundu String representation= https:// www.geeksforgeeks.org#Arnab_Kundu

相关推荐