Jeff Axelrod
Jeff Axelrod

Reputation: 28188

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

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?

Upvotes: 16

Views: 17564

Answers (3)

epox
epox

Reputation: 10900

File, in the general case, is longer

new URL("http://www.google.com/x/y/z.html?v=1#chapter1").getFile();
// returns:  "/x/y/z.html?v=1"

, than Path:

new URL("http://www.google.com/x/y/z.html?v=1#chapter1").getPath();
// returns:  "/x/y/z.html"

Upvotes: 0

Mohammod Hossain
Mohammod Hossain

Reputation: 4114

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.

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143886

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.

Upvotes: 26

Related Questions