File class in Java
In Java, a File is an abstract data type. A named location used to store related information is
known as a File. There are several File Operations like creating a new File, getting information
about File, writing into a File, reading from a File and deleting a File.
File Methods
public String getName()
public String getPath()
public String getAbsolutePath()
public String getParent()
public boolean exists() throws SecurityException
public boolean canWrite() throws SecurityException
public boolean canRead() throws SecurityException
public boolean isFile() throws SecurityException
public boolean isDirectory() throws SecurityException
public boolean isAbsolute()
public long lastModified() throws SecurityException
public long length() throws SecurityException
public boolean mkdir()
public boolean mkdirs() throws SecurityException
public boolean renameTo(File destination) throws SecurityException
public boolean delete() throws SecurityException
import [Link].*;
public class FileInfo {
public static void main(String[] args) throws IOException{
try{
1
File f = new File("e:/[Link]");
[Link]();
if ([Link]()) {
[Link]("getName: " + [Link]());
[Link]("getPath: " + [Link]());
[Link]("getAbsolutePath: " + [Link]());
[Link]("getParent: " + [Link]());
if ([Link]()) {
[Link]([Link]() + " is a file.");
if ([Link]()) {
[Link]([Link]() + " is a directory.");
else {
[Link]("What is this?");
if ([Link]()) {
[Link]([Link]() + " is an absolute path.");
else {
[Link]([Link]() + " is not an absolute path.");
}
try {
[Link]("Last Modified" + [Link]());
2
[Link]([Link]() + " is " + [Link]() + " bytes.");
catch (Exception ex) {[Link](ex);}
}catch(Exception ex){[Link]();}
3
Example of How to Create Directory in Java
Just like above example of creating file in Java we can create directory in Java, only difference is
that we need to use mkdir() method to create directory in Java
import [Link].*;
public class DirectoryExample {
public static void main(String[] args) {
boolean dirFlag = false;
// create File object
File stockDir = new File("d://Stock/ stockDir ");
try { dirFlag = [Link]();
} catch (SecurityException Se) {
[Link](Se);}
if (dirFlag)
[Link]("Directory created successfully");
else
[Link]("Directory was not created successfully");
}}
4
Instantiating a [Link]
File file = new File("c:\\data\\[Link]");
Check if File Exists
Once you have instantiated a File object you can check if the corresponding file actually exists
already. The File class constructor will not fail if the file does not already exists. To check if the
file exists, call the exists() method.
File file = new File("c:\\data\\[Link]");
boolean fileExists = [Link]();
Create a Directory if it Does Not Exist
The mkdir() method creates a single directory if it does not already exist.
The mkdirs() method creates a multiple directory if it does not already exist.
public static void main(String args[])
boolean dirFlag = false;
// create File object
File packDir = new File("e:/mypack/parkDir");
try { dirFlag = [Link]();}
catch (SecurityException Se) {[Link](Se);}
if (dirFlag)
[Link]("Directory created successfully");
else
[Link]("Directory was not created successfully");
5
File Length
To read the length of a file in bytes, call the length() method.
File file = new File("c:\\data\\[Link]");
long length = [Link]();
Rename or Move File
To rename (or move) a file, call the method renameTo() on the File class.
File file = new File("c:\\data\\[Link]");
boolean success = [Link](new File("c:\\data\\[Link]"));
Delete File
To delete a file call the delete() method. Here is a simple example:
File file = new File("c:\\data\\[Link]");
boolean success = [Link]();
Check if Path is File or Directory
File file = new File("c:\\data");
boolean isDirectory = [Link]();
Read List of Files in Directory
File file = new File("c:\\data");
String[] fileNames = [Link]();
File[] files = [Link]();