URL sameFile() method in Java with Examples Last Updated : 10 May, 2019 Comments Improve Suggest changes Like Article Like Report 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: Java // Java program to illustrate // URL.sameFile() method import java.net.*; class GFG { public static void main(String args[]) throws Exception { // create URL1 URL url1 = new URL("https:// www.geeksforgeeks.org"); // create URL2 URL url2 = new URL("https:// www.geeksforgeeks.org"); // check if two URL are same or not System.out.print("URL 1 is compared to URL 2: "); if (url1.sameFile(url2)) System.out.println("same"); else System.out.println("not same"); } } 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. Java // Java program to check the use of sameFile import java.net.*; class GFG { public static void main(String args[]) throws Exception { // create URL1 URL url1 = new URL("https:// www.geeksforgeeks.org"); // create URL2 URL url2 = new URL("https:// www.geeksforgeeks.org#print"); // check if two URL are same or not System.out.print("URL 1 is compared to URL 2: "); if (url1.sameFile(url2)) System.out.println("same"); else System.out.println("not same"); } } 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 More infoAdvertise with us Next Article URL sameFile() method in Java with Examples A andrew1234 Follow Improve Article Tags : Java Java-Functions Java-net-package Java-URL Practice Tags : Java Similar Reads Path toFile() method in Java with Examples The toFile() method of java.nio.file.Path interface used to return a java.io.File object representing this path object. if this Path is associated with the default provider, then this method returns a java.io.File object constructed with the String representation of this path. If this path was creat 2 min read Path toUri() method in Java with Examples The toUri() method of java.nio.file.Path interface used to return a URI to represent this path. This method converts this path into an absolute URI with a scheme equal to the URI scheme that identifies the provider. The exact form of the scheme-specific part is highly provider dependent. In the scen 2 min read Path resolve() method in Java with Examples resolve() method of java.nio.file.Path used to resolve the given path against this path. There are two types of resolve() methods. resolve(String other) method of java.nio.file.Path used to converts a given path string to a Path and resolves it against this Path in the exact same manner as specified 3 min read Files size() method in Java with Examples size() method of java.nio.file.Files help us to get the size of a file (in bytes). This method returns the file size, in bytes by taking the path of the file as a parameter. The size may differ from the actual size on the file system due to compression, support for sparse files, or other reasons. Th 2 min read URL getFile() method in Java with Examples The getFile() function is a part of URL class. The function getFile() returns the file name of a specified URL. The getFile() function returns the path and the query of the URL. Function Signature: public String getFile() Syntax: url.getFile() Parameter: This function does not require any parameter 2 min read Like