0% found this document useful (0 votes)
28 views6 pages

21.file in Java

The document provides an overview of the File class in Java, detailing its purpose as an abstract data type for managing files and directories. It lists various file operations and methods, such as creating, reading, writing, and deleting files, along with example code snippets demonstrating how to use these methods. Additionally, it explains how to check file existence, create directories, and retrieve file properties like length and modification date.

Uploaded by

khushi2005malik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

21.file in Java

The document provides an overview of the File class in Java, detailing its purpose as an abstract data type for managing files and directories. It lists various file operations and methods, such as creating, reading, writing, and deleting files, along with example code snippets demonstrating how to use these methods. Additionally, it explains how to check file existence, create directories, and retrieve file properties like length and modification date.

Uploaded by

khushi2005malik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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]();

You might also like