You know that data and operations on data are important parts of computer programs. Some time, your program needs to read data from file and write data to a file. Java offers various classes in a single java.io package to facilitate their input/output (I/0) operations. Java treats I/0 in the form of streams. The data flow between a program and the I/O devices through these streams.
By using streams, Java offers a standard way to perform I/0 operations on I/O device i.e. file or socket. In this chapter, we deal with files only. Before you can read from a file or write to a file, you must have a way to represent a file in your program & to navigate the file system. This is done using File class from the java.io package.
File Class
The java.io package offers the File class to make you to able to work with the file system on your machine. Using this class, you cannot only represent files but also directories.
Constructors
File class constructors provides different ways to specify the path name. Path name is an address of a file in one file system. File class constructors are summarized below:
- File (String path Name)
- File (String parent, String child)
- File (File parent, String child)
Implementation:
The following program demonstrates the use of different constructors to refer a file or directory. It also shows the use of the method getCanonicalPath() which returns system dependent abstract path:
import Java.io.*;
class FileConstructors
{
public static void main(String args[])
{
try
{
File F1 = new File("PDIR/CDIR");
File F2 = new File("PDIR/CDIR","data/file.txt");
File F3 = new File(F1,"data/file2.txt");
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
Explanation:
The getCanonicalPath() method returns the system dependent abstract path name, if this program is saved in C:/JDK/Bin> directory. It generates the following output:
Output:
F1: Pathname = C:/JDK/Bin/PDIR/CDIR
F2: Pathname = C:/JDK/Bin/PDIR/CDIR/data/file1.txt
F3: Pathname = C:/JDK/Bin/PDIR/CDIR/data/file2.txt
Methods Provided in File Class
Once you create an instance of File class, then you can use the following methods of File class to perform various navigational operations such as creation, deletion renaming of file etc. Some commonly used methods of File class are shown in the following table:
| Methods |
Description |
| boolean createNewFile() |
Creates new file, if the file name at specified path doesn't exist |
| boolean exists() |
It returns true, if file or directory is associated with file instance exist, else returns true. |
| boolean delete() |
Deletes the file or directory represented by the file instance. |
| boolean isDirectory () |
Returns true, if the file instance represents a directory |
| boolean isFile() |
Returns true, if the file instance represents a file. |
| String[] list() |
Returns the list of files and directories contained by a directory represented by the File instance. |
| File[] listFiles() |
Returns the list of files & directories contained in the directory represented by this File instance. |
| boolean mkdir() |
Creates a directory at specified path. |
| boolean renameTo(File object) |
Renames the file or directory represented by this File instance to new name specified in new File instance. |
| boolean canRead() |
Returns true if the file or directory represented by file instance is readable by program, else returns false. |
| boolean canWrite() |
Returns true if the File directory associated with File instance can be modified |
| String getName() |
Returns name of file and directory represented by File instance. |
//FileClass_Fx_3.java
//Program to display list of files and directories
import java.io.File;
public class FileClass_Fx_3
{
public static void main(String []args)
{
File ff=new File("c:\\windows");
int fcount=0,dcount=0;
File []listFile=ff.listFile();
for(File s: listFile)
{
if(s.isFile())
{
System.out.println("File Name="+s);
fcount++;
}
else
{
System.out.println(">Dir Name="+s);
dcount++;
}
}
System.out.println("File="+fcount+"& Directories="+dcount);
}
}
Output
Dir Name=c:\windows\addins
> Dir Name=c:\windows\AppCompat
> Dir Name=c:\windows\AppPatch
> Dir Name=c:\windows\assembly
File Name=c:\windows\atiogl.xml
File Name=c:\windows\ativpsrm.bin
…..
…..
Files=41 & Directories=58
//FileClass_Fx_2.java
//Program to delete a file
import java.io.File;
public class FileClassFx_2
{
public static void main(String []args)
{
File delFile=new File("c:\\chk4.java");
boolean b=delFile.delete();
if(b==true)
System.out.Println("File deleted");
else
System.out.println("File not Found");
}
}
Output
File deleted
//FileClass_Fx_1.java
//Program using various methods of File class
import java.io.*;
public class FileClassFx_1
{
public static void main(String[] args)
{
try
{
File fff=new File("c://chk.java");
System.out.println("Is it Exist:"+fff.exist());
boolean b=fff.createNewFile();
if(b==true)
System.out.println("File Created");
else
System.out.println("File ALready Exist");
System.out.println("Is it Exist Now:"+fff.exist()+"|");
System.out.println("Parent="+fff.getParent()+"|");
System.out.println("Parent="+fff.isDirectory()+"|");
System.out.println("isFile="+fff.isFile());
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Output:
Is it Exist:false
File Created...
Is it Exist Now : true | Parent=c:\ | isDir=false| is File=true
//FileClass_Fx_4.java
//Program to create a directory and file'
import java.io.*;
public class FileClass_Fx_4{
public static void main(String[] args)
{
try
{
File nwDir=new File("c:\\creator");
boolean b=nwDir.mkdir();
if(b==true)
System.out.println("Directory Created ...");
else
System.out.println("File Already Exist...");
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
Output
Directory Created...
File Created....