Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URL getFile() Method with Examples



Description

The Java URL getFile() method returns the file name of this URL. The returned file portion will be the same as getPath(), plus the concatenation of the value of getQuery(), if any. If there is no query portion, this method and getPath() will return identical results.

Declaration

Following is the declaration for java.net.URL.getFile() method

public String getFile()

Parameters

NA

Return Value

the file name of this URL, or an empty string if one does not exist.

Exception

NA

Example 1

The following example shows the usage of Java URL getFile() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Now using getFile() method, we're getting the file name and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https","www.tutorialspoint.com","/index.htm");
         String content = url.getFile();
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

/index.htm

Example 2

The following example shows the usage of Java URL getFile() method for a valid url with query parameters. In this example, we're creating an instance of URL class. Now using getFile() method, we're getting the file name and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
         String fileName = url.getFile();
         System.out.println(fileName);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

/index.htm?language=en

Example 3

The following example shows the usage of Java URL getFile() method for a valid url with file protocol. In this example, we're creating an instance of URL class. Now using getFile() method, we're getting the file name and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;

public class UrlDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("file","www.tutorialspoint.com","/index.htm");
         String fileName = url.getFile();
         System.out.println(fileName);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

/index.htm
java_url.htm
Advertisements