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 |
Boolean | Is the file (or directory) readable? | |
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. |
boolean | Does the file (or directory) exist? | |
getName( ) | String | Returns the name of the file (or directory). |
getParent( ) | String | |
String | Returns the path of the file (or directory). | |
boolean | Is the item a directory? | |
boolean | Is the item a file? | |
boolean | Is the item hidden? (System-dependent.) | |
long | Returns the last modification time of the file (or directory). | |
long | Returns the length of the file. | |
String [] | Returns a list of files in the directory. | |
File[] | Returns the contents of the directory as an array of File objects. | |
File[] | Returns array of root filesystems if any (e.g., C:/, D:/). | |
boolean | Creates the directory. | |
boolean | Creates all directories in the path. | |
renameTo(File dest) | boolean | Renames the file (or directory). |
boolean | Sets the last-modified time of the file (or directory). | |
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