Java.io.File Class in Java
The File class is Java’s representation of a file or directory path name. Because file and directory names have different formats on different platforms, a simple string is not adequate to name them. The File class contains several methods for working with the path name, deleting and renaming files, creating new directories, listing the contents of a directory, and determining several common attributes of files and directories.
- It is an abstract representation of file and directory pathnames.
- A pathname, whether abstract or in string form can be either absolute or relative. The parent of an abstract pathname may be obtained by invoking the getParent() method of this class.
- First of all, we should create the File class object by passing the filename or directory name to it. A file system may implement restrictions to certain operations on the actual file-system object, such as reading, writing, and executing. These restrictions are collectively known as access permissions.
- Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.
How to create a File Object?
A File object is created by passing in a String that represents the name of a file, or a String or another File object. For example,
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
File a = new File("/usr/local/bin/geeks");
defines an abstract file name for the geeks file in directory /usr/local/bin. This is an absolute abstract file name.
- File(File parent, String child) : Creates a new File instance from a parent abstract pathname and a child pathname string.
- File(String pathname) : Creates a new File instance by converting the given pathname string into an abstract pathname.
- File(String parent, String child) : Creates a new File instance from a parent pathname string and a child pathname string.
- File(URI uri) : Creates a new File instance by converting the given file: URI into an abstract pathname.
- boolean canExecute() : Tests whether the application can execute the file denoted by this abstract pathname.
- boolean canRead() : Tests whether the application can read the file denoted by this abstract pathname.
- boolean canWrite() : Tests whether the application can modify the file denoted by this abstract pathname.
- int compareTo(File pathname) : Compares two abstract pathnames lexicographically.
- boolean createNewFile() : Atomically creates a new, empty file named by this abstract pathname .
- static File createTempFile(String prefix, String suffix) : Creates an empty file in the default temporary-file directory.
- boolean delete() : Deletes the file or directory denoted by this abstract pathname.
- boolean equals(Object obj) : Tests this abstract pathname for equality with the given object.
- boolean exists() : Tests whether the file or directory denoted by this abstract pathname exists.
- String getAbsolutePath() : Returns the absolute pathname string of this abstract pathname.
- long getFreeSpace() : Returns the number of unallocated bytes in the partition .
- String getName() : Returns the name of the file or directory denoted by this abstract pathname.
- String getParent() : Returns the pathname string of this abstract pathname’s parent.
- File getParentFile() : Returns the abstract pathname of this abstract pathname’s parent.
- String getPath() : Converts this abstract pathname into a pathname string.
- boolean isDirectory() : Tests whether the file denoted by this pathname is a directory.
- boolean isFile() : Tests whether the file denoted by this abstract pathname is a normal file.
- boolean isHidden() : Tests whether the file named by this abstract pathname is a hidden file.
- long length() : Returns the length of the file denoted by this abstract pathname.
- String list() : Returns an array of strings naming the files and directories in the directory .
- File listFiles() : Returns an array of abstract pathnames denoting the files in the directory.
- boolean mkdir() : Creates the directory named by this abstract pathname.
- boolean renameTo(File dest) : Renames the file denoted by this abstract pathname.
- boolean setExecutable(boolean executable) : A convenience method to set the owner’s execute permission.
- boolean setReadable(boolean readable) : A convenience method to set the owner’s read permission.
- boolean setReadable(boolean readable, boolean ownerOnly) : Sets the owner’s or everybody’s read permission.
- boolean setReadOnly() : Marks the file or directory named so that only read operations are allowed.
- boolean setWritable(boolean writable) : A convenience method to set the owner’s write permission.
- String toString() : Returns the pathname string of this abstract pathname.
- URI toURI() : Constructs a file URI that represents this abstract pathname.
Program 1: Program to check if a file or directory physically exist or not.
File name :file.txt Path: file.txt Absolute path:C:\Users\akki\IdeaProjects\codewriting\src\file.txt Parent:null Exists :true Is writeable:true Is readabletrue Is a directory:false File Size in bytes 20
Program 2: Program to display all the contents of a directory
Here we will accept a directory name from the keyboard and then display all the contents of the directory .For this purpose, list() method can be used as:
In the preceding statement , the list() method causes all the directory entries copied into the array arr. Then pass these array elements arr[i] to File object and test them to know if they represent a file or directory .
Enter dirpath: C:\Users\akki\IdeaProjects\ Enter the dirname codewriting .idea : is a directory an1.txt : is a file codewriting.iml : is a file file.txt : is a file out : is a directory src : is a directory text : is a file No of entries in this directory 7
Related Post : FileReader and FileWriter in Java
This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.