File Class in Java

The java.io.File class encapsulates access to information about a file or directory. It can be used to get attribute information about a file, list the entries in a directory, and perform basic filesystem operations, such as removing a file or making a directory. While the File object handles these "meta" operations, it doesn't provide the API for reading and writing file data; there are specialized streams for that purpose. 

The following table summarizes the methods provided by the File class.


Method
Return type
Meaning
canRead( )
Boolean
Is the file (or directory) readable?
canWrite( )
Boolean
Is the file (or directory) writable?
createNewFile( )
Boolean
Creates a new file.
createTempFile (String pfx, String sfx)
File
Static method to create a new file, with the specified prefix and suffix, in the default temp file directory.
delete( )
Boolean
Deletes the file (or directory).
deleteOnExit( )
Void
When it exits, Java runtime system deletes the file.
exists( )
boolean
Does the file (or directory) exist?




getName( )
String
Returns the name of the file (or directory).
getParent( )
String
Returns the name of the parent directory of the file (or directory).
getPath( )
String
Returns the path of the file (or directory).


isDirectory( )
boolean
Is the item a directory?
isFile( )
boolean
Is the item a file?
isHidden( )
boolean
Is the item hidden? (System-dependent.)
lastModified( )
long
Returns the last modification time of the file (or directory).
length( )
long
Returns the length of the file.
list( )
String []
Returns a list of files in the directory.
listFiles( )
File[]
Returns the contents of the directory as an array of File objects.
listRoots( )
File[]
Returns array of root filesystems if any (e.g., C:/, D:/).
mkdir( )
boolean
Creates the directory.
mkdirs( )
boolean
Creates all directories in the path.
renameTo(File dest)
boolean
Renames the file (or directory).
setLastModified( )
boolean
Sets the last-modified time of the file (or directory).
setReadOnly( )
boolean
Sets the file to read-only status.




Example:

import java.io.File;

/**
 *
 * @author JavaHotSpot
 */

public class FileClass
{
 public static void main(String args[])
 {
     File f;
     f=new File("C:\\p1.txt");
     if(f.isFile())
     {
         System.out.println("File: "+f.getName()+ " is a File");
     }
     else
     {
         System.out.println("File: "+f.getName()+ " is a Directory");
     }

     System.out.println("The Size of the File: "+f.length());
     System.out.println("The Path of the File: "+f.getPath());
     System.out.println("The Parent of the File: "+f.getParent());
     System.out.println("The File was last modified: "+f.lastModified());
     if(f.exists())
     {
         System.out.println("The File is exists");
     }
     else
     {
         System.out.println("The File does not exists");
     }
     if(f.canRead())
     {
         System.out.println("The File can be read");
     }
     else
     {
         System.out.println("The File can not be read");
     }
     if(f.canWrite())
     {
          System.out.println("The File can be write");
     }
     else
     {
         System.out.println("The File can not be write");
     }
     if(f.canExecute())
     {
         System.out.println("The File can be executable");
     }
     else
     {
         System.out.println("The File can not be executable");
     }
 }
}

Output:
File: p1.txt is a File
The Size of the File: 41
The Path of the File: C:\p1.txt
The Parent of the File: C:\
The File was last modified: 1306337871062
The File is exists
The File can be read
The File can be write
The File can be executable