| Most of the classes defined by java.io operate on streams, the File class does not. It deals directly with files and the file system. That is, the File class does not specify how information is retrieved from or stored in files; it describes the properties of a file itself. A File object is used to obtain or manipulate the information associated with a disk file, such as the permissions, time, date, and directory path, and to navigate subdirectory hierarchies. Files are a primary source and destination for data within many programs. Although there are severe restrictions on their use within applets for security reasons, files are still a central resource for storing persistent and shared information.
There are different constructors can be used to create File objects:
File(File parent, String child)
File(String pathname)
File(String parent, String child)
File(URI uri)
In all these four cases ,directoryPath is the path name of the file, filename is the name of the file, dirObj is a File object that specifies a directory, and uriObj is a URI object that describes a file.
File defines various methods through which we can easily obtain the required information related to any file . For example:
1. getName ( ) method is used to get the name of a particular file
2. getParent ( ) method gives the name of the parent directory
exists ( ) method return the value “ true “ if a file exists else gives “ false” as result if it doesn’t find the file of that name which it is trying to locate a particular file.
3. isFile ( ) return true if it is called on a file and false if called on a directory
isAbsolute ( ) method return “ true” if the file is of absolute type end returns “ false” if the file is of relative type.
4. renameTo( ) methiod is used to change the name of an existing file
boolean renameTo(File newName)
It will return the value as “true” if it successfully changes the name of the file else returns “false” if it is not bale to locate the file which has to be changed.
5. delete( ) This method is used to delete a particular file and it return value as “ true” if it successfully deletes the false else returns “false” if it fails in its attempt.
Boolean delete ( )
6. deleteOnExit( ) This method removes the file associated with the invoking object when the Java Virtual Machine terminates.
7. isHidden( ) Returns true if the particular file is hidden. And gives value as false in the other case.
We will show you different usage of java.io.File by small code snipnet. First we will look how to create a file in Java
public class FileCreate {
public static void main() {
File file = new File("myfile.txt"); // this creates a new file
}
}
This will create a file named "myfile.txt" in the current directory from where you will execute this code.
Below example displays the usage of other file APIs.
import java.io.File;
import java.io.IOException;
public class RenameToExample {
public static void main(String[] args) throws IOException {
File file = new File("myfile.txt");
file.createNewFile();
System.out.println(file.getAbsoluteFile());
System.out.println(file.getCanonicalFile());
System.out.println(file.getName());
System.out.println(file.getPath());
System.out.println(file.canRead());
System.out.println(file.canWrite());
System.out.println(file.canExecute());
System.out.println(file.isDirectory());
System.out.println(file.isHidden());
file.renameTo(new File("rename.txt"));
}
}
|