Java Reference
In-Depth Information
public int getPort()
The getPort() method returns the port number specified in the URL as an int . If no
port was specified in the URL , getPort() returns -1 to signify that the URL does not
specify the port explicitly, and will use the default port for the protocol. For example, if
the URL is http://www.userfriendly.org/ , getPort() returns -1; if the URL is http://
www.userfriendly.org:80/ , getPort() returns 80. The following code prints -1 for the
port number because it isn't specified in the URL :
URL u = new URL ( "http://www.ncsa.illinois.edu/AboutUs/" );
System . out . println ( "The port part of " + u + " is " + u . getPort ());
public int getDefaultPort()
The getDefaultPort() method returns the default port used for this URL 's protocol
when none is specified in the URL. If no default port is defined for the protocol, then
getDefaultPort() returns -1. For example, if the URL is http://www.userfriendly.org/ ,
getDefaultPort() returns 80; if the URL is ftp://ftp.userfriendly.org:8000/ , getDefault
Port() returns 21.
public String getFile()
The getFile() method returns a String that contains the path portion of a URL; re‐
member that Java does not break a URL into separate path and file parts. Everything
from the first slash (/) after the hostname until the character preceding the # sign that
begins a fragment identifier is considered to be part of the file. For example:
URL page = this . getDocumentBase ();
System . out . println ( "This page's path is " + page . getFile ());
If the URL does not have a file part, Java sets the file to the empty string.
public String getPath()
The getPath() method is a near synonym for getFile() ; that is, it returns a String
containing the path and file portion of a URL. However, unlike getFile() , it does not
include the query string in the String it returns, just the path.
Note that the getPath() method does not return only the directory
path and getFile() does not return only the filename, as you might
expect. Both getPath() and getFile() return the full path and file‐
name. The only difference is that getFile() also returns the query
string and getPath() does not.
Search WWH ::




Custom Search