
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - FilePermission hashCode() method
Description
The Java FilePermission hashCode() method returns a hash code value for the FilePermission object. This hash code is primarily used for efficient storage in hash-based collections like HashSet and HashMap. The hash code is derived from the file path and permission actions.
Declaration
Following is the declaration for java.io.FilePermission.hashCode() method −
public int hashCode()
Parameters
NA
Return Value
This method returns the hash code value of this object.
Exception
NA
Example - Usage of FilePermission hashCode() method
The following example shows the usage of Java FilePermission hashCode() method.
FilePermissionDemo.java
package com.tutorialspoint; import java.io.FilePermission; import java.io.IOException; public class FilePermissionDemo { public static void main(String[] args) throws IOException { FilePermission fp = null; try { // create new file permissions fp = new FilePermission("test.txt", "read"); // the hash code value int hash = fp.hashCode(); // prints System.out.print("Hash Code: "+hash); } catch(Exception ex) { // if an error occurs ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Hash Code: 1006542423
Example - Hash Code for Read and Write Permissions
The following example shows the usage of Java FilePermission hashCode() method.
FilePermissionDemo.java
package com.tutorialspoint; import java.io.FilePermission; public class FilePermissionDemo { public static void main(String[] args) { // Creating a FilePermission object with "read" and "write" actions FilePermission filePermission = new FilePermission("example.txt", "read,write"); // Getting and displaying the hash code int hash = filePermission.hashCode(); System.out.println("Hash Code: " + hash); } }
Output (varies at runtime)
Let us compile and run the above program, this will produce the following result−
Hash Code: 579275665
Explanation
A FilePermission object is created for "example.txt" with "read" and "write" permissions.
The hashCode() method generates a unique integer based on the file path and permission actions.
Example - Different Permissions Result in Different Hash Codes
The following example shows the usage of Java FilePermission hashCode() method.
FilePermissionDemo.java
package com.tutorialspoint; import java.io.FilePermission; public class FilePermissionDemo { public static void main(String[] args) { // Creating two FilePermission objects with different actions FilePermission filePermission1 = new FilePermission("example.txt", "read"); FilePermission filePermission2 = new FilePermission("example.txt", "write"); // Getting and displaying the hash codes System.out.println("Hash Code (read): " + filePermission1.hashCode()); System.out.println("Hash Code (write): " + filePermission2.hashCode()); } }
Output (varies at runtime)
Let us compile and run the above program, this will produce the following result−
Hash Code (read): -1195731697 Hash Code (write): 1324228237
Explanation
Two FilePermission objects are created for the same file (example.txt) but with different actions (read and write).
Since permissions differ, the hashCode() values will also differ.
If two FilePermission objects have the same file path and actions, they will generate the same hash code.