Java - File hashCode() Method



Description

The Java File hashCode() method computes and returns a hash code for this abstract name.

Declaration

Following is the declaration for java.io.File.hashCode() method −

public int hashCode()

Parameters

NA

Return Value

The method returns a hash code for this abstract pathname.

Exception

NA

Example 1

The following example shows the usage of Java File hashCode() method. We've created a File reference. Then we're creating a File Object using test.txt which is not present in the current directory. Then we've created the file using createNewFile() method. Now using hashCode() method, printing the hashCode of the file object.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      
      try {
         // create new files
         f = new File("test.txt");
         
         // create new file in the system
         f.createNewFile();
         
         // prints hash code
         System.out.println("hash code: "+f.hashCode());
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

hash code: -1148812667

Example 2

The following example shows the usage of Java File hashCode() method. We've created a File reference. Then we're creating a File Object using F:/test.txt which is present in the provided directory. Now using hashCode() method, we're printing the hashcode.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      
      try {
         // create new files
         f = new File("F:/test.txt");         

         // prints hash code
         System.out.println("File exists: "+f.hashCode());
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

File exists: 54783902

Example 3

The following example shows the usage of Java File hashCode() method. We've created a File reference. Then we're creating a File Object using F:/test directory which is present in the provided location. Now using hashCode() method, we're printing the hashcode.

package com.tutorialspoint;
import java.io.File;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;     
      try {
         
         // create new files
         f = new File("F:/test");         
         
         // prints
         System.out.println("hash code: "+f.hashCode());
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

hash code: -592770916
java_file_class.htm
Advertisements