16

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?

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246

3 Answers3

26

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.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
1

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.

Tom
  • 16,842
  • 17
  • 45
  • 54
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
0

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"
epox
  • 9,236
  • 1
  • 55
  • 38