Java - URL sameFile(URL other) Method with Examples
Description
The Java URL sameFile(URL other) method compares two URLs, excluding the fragment component. It returns true if this URL and the other argument are equal without taking the fragment component into consideration.
Declaration
Following is the declaration for java.net.sameFile(URL other) method
Compares two URLs, excluding the fragment component.
Parameters
other − the URL to compare against.
Return Value
true if they reference the same remote object; false otherwise.
Exception
NA
Example 1
The following example shows the usage of Java URL sameFile(URL e) method. In this example, we're creating two instances of URL classes with same url. Now using sameFile() method we're comparing both the objects. As being object based on same url, result is as expected and verified in output as shown below −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
public class UrlDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
URL urlToCompare = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
System.out.println(url.sameFile(urlToCompare));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
true
Example 2
The following example shows the usage of Java URL sameFile(URL e) method. In this example, we're creating two instances of URL classes with same url with different fragment. Now using sameFile() method we're comparing both the objects. As being object based on same url, result is as expected and verified in output as shown below −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
public class UrlDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
URL urlToCompare = new URL("https://www.tutorialspoint.com/index.htm?language=en");
System.out.println(url.sameFile(urlToCompare));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
true
Example 3
The following example shows the usage of Java URL equals(Object e) method. In this example, we're creating two instances of URL classes with same url and in another instance, we're also passing host and file name. Now using sameFile method we're comparing both the objects. As being object are not same now, result is as expected and verified in output as shown below −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
public class UrlDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com/index.htm?language=en#j2se");
URL urlToCompare = new URL("https", "https://www.tutorialspoint.com","index.htm?language=en#j2se" );
System.out.println(url.sameFile(urlToCompare));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
false