URL getFile() method in Java with Examples

The getFile() function is a part of URL class. The function getFile() returns the file name of a specified URL. The getFile() function returns the path and the query of the URL.

Function Signature:

public String getFile()

Syntax:



url.getFile()

Parameter: This function does not require any parameter

Return Type: The function returns String Type

Below programs illustrates the use of getFile() function:

Example 1: Given a URL we will get the file using the getFile() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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

Example 2: Now see how getFile() is different from getPath(). getPath() will exclude the query but getFile() will include the query

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
        }
    }
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.