Java URI getFile()方法及示例

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

Java URI getFile()方法及示例

getFile()函数是URL类的一部分。getFile()函数返回一个指定的URL的文件名。getFile()函数返回URL的路径和查询。

函数签名。

public String getFile()

语法:

url.getFile()

参数 :该函数不需要任何参数

返回类型 :该函数返回字符串类型

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

例1 :给定一个URL,我们将使用getFile()函数获得文件。

// Java program to show the// use of the function getFile()  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/url-getprotocol-method-in-java-with-examples/");              // get the  File            String _File = url.getFile();              // display the URL            System.out.println("URL = " + url);              // display the  File            System.out.println(" File= " + _File);        }          // if any error occurs        catch (Exception e) {              // display the error            System.out.println(e);        }    }}

输出:

URL = https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples/ File= /url-getprotocol-method-in-java-with-examples/

例2 :现在看看getFile()和getPath()有什么不同。getPath()将排除查询,但getFile()将包括查询。

// Java program to show the// use of the function getFile()  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/url-getprotocol-method-in-java-with-examples?title=protocol");              // get the  File            String _File = url.getFile();              // display the URL            System.out.println("URL = " + url);              // display the  File            System.out.println(" File= " + _File);        }          // if any error occurs        catch (Exception e) {              // display the error            System.out.println(e);        }    }}

输出:

URL = https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol File= /url-getprotocol-method-in-java-with-examples?title=protocol

相关推荐