Java File
Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations on files and directories. It is a representation of file and directory pathnames that is abstract. Instances of the File class are immutable, which means that once generated, a File object's abstract pathname will never change. This class comes under the java.io package, and it implements the following interfaces.
- Serializable
- Comparable<File>
File and Directory
A file is a name for a location where relevant data can be saved. And a directory is a part or place in your computer’s memory where you can store files. It is implemented to perform two functions: to store other directories ( known as subdirectories) and helps keep the computer organized.
File Class Constructors
File class provides the following constructors, such as:
1 |
File(Filepnt, String cld) This constructor is implemented when there is a need to create a new file instance that must include the following parameters: parent abstract pathname and a child pathname string. |
2 |
File(Stringpathn) When a new File instance must be created, this constructor is implemented to turn the provided pathname string into an abstract pathname. |
3 |
File(String pnt, String cld) This constructor is implemented when there is a need to create a new file instance must be created by using three parameters: string, parent pathname, and child pathname. |
4 |
File(URI u) This constructor is used when a new file instance has to be generated by transforming the following parameters: URI into an abstract pathname. |
File Class Methods
File class provides the following methods.
- canExecute()
This function is implemented when it is necessary to check whether the application can carry out its function to execute the file represented by this abstract pathname. This method has a boolean return type.
- canRead()
This function is implemented when it is necessary to check whether the application can carry out its function to read the file represented by this abstract pathname. This method has a boolean return type.
- canWrite()
This function is implemented when it is necessary to check whether the application can carry out its function to update the file represented by this abstract pathname. This method has a boolean return type.
- compareTo(Filepname)
This method is implemented when there is a need to differentiate two abstract pathnames lexicographically. This method has an int return type.
- createNewFile()
This method is implemented when there is a need to generate a new empty file, represented by this abstract pathname. This method has a boolean return type.
- createTempFile(String pf, String sf )
This method is implemented when there is a need to generate a new file that is empty and has a default temporary-file directory. This method has a file return type.
- delete()
This method is implemented when there is a need to delete the file or directory represented by this abstract pathname. This method has a boolean return type.
- equals(Object o)
This method is implemented when there is a need to examine the abstract pathname for equality with the specified object. This method has a boolean return type.
- exists()
This method is implemented when there is a need to examine whether the file or directory denoted by this abstract pathname exists. This method has a boolean return type.
- getAbsolutePath()
This function is implemented when the absolute pathname string of this abstract pathname must be returned. This method has a String return type.
- list()
As its name implies, this method produces an array of strings naming the files and folders in the directory. This method has a String[ ] return type.
- getFreeSpace()
This function is used when the number of unallocated bytes in the partition must be returned. This method has a long return type.
- getName()
This method is implemented when there is a need to return the file or directory name denoted by this abstract pathname. This method has a String return type.
- getParent()
This method is implemented when there is a need to return the pathname string of this abstract pathname’s parent. This method has a String return type.
- getParentFile()
This method is implemented when there is a need to return the abstract pathname of this abstract pathname’s parent. This method has a String return type.
- getPath()
This method is implemented when there is a need to convert this abstract pathname into a pathname string. This method has a String return type.
- setReadOnly()
This method is implemented when there is a need to mark the file or directory name, ensuring that only read operations can be implemented. This method has a boolean return type.
- isDirectory()
This method is implemented when there is a need to examine the file denoted by this pathname as a directory. This method has a String return type.
- isFile()
This method is implemented when there is a need to examine the file denoted by this abstract pathname as a normal file. This method has a String return type.
- isHidden()
This method is implemented when there is a need to examine whether the file represented by this abstract pathname is hidden. This method has a String return type.
- length( )
As the function's name implies, this function returns the length. The length belongs to the file denoted by this abstract pathname. This method has a long return type.
- listFiles( )
This function returns an array that belongs to the abstract pathnames, which indicate the particular files in the directory. This method has a file[ ] return type.
- mkdir()
This function generates a directory named by this abstract pathname. This method has a String return type.
- renameTo(File dt )
As the name implies, this function renames the file represented by this abstract pathname. This method has a boolean return type.
- setExecutable(boolean executable)
This function is a quick way to change the owner's execute permission. This method has a String return type.
- setReadable(boolean readable)
This function is a quick way to change the owner's read permission. This method has a String return type.
- setReadable(boolean rd, boolean only)
This function is a quick way to change the owner's, or everybody’s read permission. This method has a boolean return type.
- setWritable(boolean w)
This function is a quick way to change the owner's write permissions. This method has a boolean return type.
- toString()
This method is implemented when there is a need to return the pathname, which is a string type belonging to this abstract pathname. This method has a String return type.
- toURI()
This method is implemented when it's necessary to produce a file URI that indicates this abstract pathname. This method has a URI return type.