Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between url.getFile() and getpath()?

Tags:

java

http

url

path

In java.net.url there is a getFile() method and a getPath() method.

In my testing, they both return the same result: the full path and file after the domain name trailing slash.

For instance, http://www.google.com/x/y/z.html returns x/y/z.html for both methods.

Could someone elaborate on the Javadocs?

like image 419
Jeff Axelrod Avatar asked Aug 13 '12 04:08

Jeff Axelrod


People also ask

What is getFile in Java?

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.

What is file getPath in Java?

The getPath() method is a part of File class. This function returns the path of the given file object. The function returns a string object which contains the path of the given file object.

What does getPath return?

Simply put, getPath() returns the String representation of the file's abstract pathname. This is essentially the pathname passed to the File constructor. So, if the File object was created using a relative path, the returned value from getPath() method would also be a relative path.


2 Answers

URL.getFile():

Gets 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.

like image 25
Mohammod Hossain Avatar answered Sep 23 '22 02:09

Mohammod Hossain


The URL.getFile() javadocs say this:

Gets 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.

They will be the same unless there is a query string, e.g. a ?somename=value&somethingelse=value2 in the URL.

like image 200
Jon Lin Avatar answered Sep 21 '22 02:09

Jon Lin