VOOZH about

URL: https://www.geeksforgeeks.org/java/url-samefile-method-in-java-with-examples/

⇱ URL sameFile() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

URL sameFile() method in Java with Examples

Last Updated : 11 Jul, 2025
The sameFile() function of Java.net.URL class is used to compare two URLs excluding the fragment part. This method returns true if both the URL are same excluding the fragment part else returns false. Function Signature
public boolean sameFile(URL u)
Syntax
url1.sameFile(url2);
Parameter: This method accepts a mandatory parameter url which is the second url to be compared to this url. Return Value: This method returns true if both the URL are same excluding the fragment part else returns false. Below methods illustrate URL.sameFile() method: Example 1:
Output:
URL 1 is compared to URL 2: same
Example 2: The sameFile() function has a specific use that makes it different from the equals() function. The sameFile() function compares the URL excluding the fragment part. The following example will illustrate the use which makes it different from equals function.
Output:
URL 1 is compared to URL 2: same
Note: If equals function would have been used, then the second code would have printed "not same" but using the sameFile() function it will give result "same".
Comment