What is the diffecence between getFile() and getPath() of java.net.URL oject?
Asked
Active
Viewed 256 times
2 Answers
1
From the Javadoc https://docs.oracle.com/javase/8/docs/api/java/net/URL.html
public String 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.
so getPath() might be
/path/file.php
and the getFile() might be
/path/file.php?query=hello

Peter Lawrey
- 525,659
- 79
- 751
- 1,130
-1
getFile()
will return the name of the file:
MyFile.txt
getPath()
returns the path to the file:
C:/MyFolder/MyFile.txt
-
1That's not what the Javadoc suggests. – Peter Lawrey Apr 15 '16 at 13:29
-
@PeterLawrey getFile() Gets the file name of this URL. getPath() Gets the path part of this URL. That is the JavaDoc, so afaik, yes, it does – Stultuske Apr 15 '16 at 13:30
-
@Stultuske Then check Peters answer. Or this code example: `URL u = new URL("http", "blub.com", 123, "test/sub/blub.txt?opt=1"); System.out.println(u.getFile()); System.out.println(u.getPath());`. Even if you use `u = new File("blub.txt").toURL()` you would get that result. – Tom Apr 15 '16 at 13:32