You are on page 1of 5

Java.io.

File Class in Java


 The File class is Java’s representation of a file or directory path name.
 Because file and directory names have different formats on different platforms, a simple string is not
adequate to name them.
 The File class contains several methods for working with the path name, deleting and renaming files,
creating new directories, listing the contents of a directory, and determining several common
attributes of files and directories.

It is an abstract representation of file and directory pathnames.

 A pathname, whether abstract or in string form can be either absolute or relative. The parent of an
abstract pathname may be obtained by invoking the getParent() method of this class.

 First of all, we should create the File class object by passing the filename or directory name to it.
A file system may implement restrictions to certain operations on the actual file-system object, such
as reading, writing, and executing. These restrictions are collectively known as access permissions.
 Instances of the File class are immutable; that is, once created, the abstract pathname represented by
a File object will never change.
How to create a File Object?
A File object is created by passing in a String that represents the name of a file, or a String or another File
object. For example,
File a = new File("/usr/local/bin/abc.txt");

EXAMPLE OF FILE CLASS

class fileProperty
{
public static void main(String[] args)
{
//accept file name or directory name through command line args
//String fname =args[0];

//pass the filename or directory name to File object


File f = new File("A.java");

//apply File class methods on File object


System.out.println("File name :"+f.getName());
System.out.println("Path: "+f.getPath());
System.out.println("Absolute path:" +f.getAbsolutePath());
System.out.println("Parent:"+f.getParent());
System.out.println("Exists :"+f.exists());
if(f.exists())
{
System.out.println("Is writeable:"+f.canWrite());
System.out.println("Is readable"+f.canRead());
System.out.println("Is a directory:"+f.isDirectory());
System.out.println("File Size in bytes "+f.length());
}
}
}
Output:-
Path: A.java
Absolute path:/home/hjd/Desktop/A.java
Parent:null
Exists :true
Is writeable:true
Is readabletrue
Is a directory:false
File Size in bytes 108

Program 2: Program to display all the contents of a directory


Here we will accept a directory name from the keyboard and then display all the contents of the directory
.For this purpose, list() method can be used as:
String arr[]=f.list();

import java.io.*;
public class FileExample
{
public static void main(String[] args)
{
File f=new File("/home/hjd/Desktop");
String filenames[]=f.list();
for(String filename:filenames)
{
System.out.println(filename);
}
}
}

Java File Example 3:- List all Files and Directories on Location
import java.io.*;
public class FileExample
{
public static void main(String[] args)
{
File dir=new File("/home/hjd/Desktop");
File files[]=dir.listFiles();
for(File file:files)
{
System.out.println(file.getName()+" Can Write: "+file.canWrite()+ "Is Hidden: "
+file.isHidden()+" Length: "+file.length()+" bytes");
}
}
}

Java FilterInputStream Class


Java FilterInputStream class implements the InputStream. It contains different sub classes as
BufferedInputStream, DataInputStream for providing additional functionality. So it is less used individually.

Example of FilterInputStream class

Import java.io.*;
public class FilterExample
{
public static void main(String[] args) throws IOException
{
File data = new File("D:\\testout.txt");
FileInputStream file=new FileInputStream(data);
FilterInputStream filter = new BufferedInputStream(file);
int k =0;
while((k=filter.read())!=-1)
{
System.out.print((char)k);
}
file.close();
filter.close();
}
}

InputStreamReader class
InputStreamReader class can be used to read data from keyboard.It performs two tasks:

 connects to input stream of keyboard


 converts the byte-oriented stream into character-oriented stream

BufferedReader class
BufferedReader class can be used to read data line by line by readLine() method.

Example of reading data from keyboard by InputStreamReader and BufferdReader


class
In this example, we are connecting the BufferedReader stream with the InputStreamReader stream for
reading the line by line data from the keyboard.
import java.io.*;
class abc
{
public tatic void main (String args[]) throws Exception
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
System.out.println(“Enter Your Name:-”);
String name=br.readLine();
System.out.println(“Welcome “ +name);
}
}
Another Example of reading data from keyboard by InputStreamReader and
BufferdReader class until the user writes stop

Import java.io.*;
class abc
{
public static void main(String args[])throws Exception
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String name="";
while(!name.equals("stop"))
{
System.out.println("Enter data: ");
name=br.readLine();
System.out.println("data is: "+name);
}
br.close();
r.close();
}
}

Java FileReader Class


 Java FileReader class is used to read data from the file. It returns data in byte format like
FileInputStream class.

 It is character-oriented class which is used for file handling in java.

Java FileReader class declaration


Let's see the declaration for Java.io.FileReader class:
public class FileReader extends InputStreamReader

Methods of FileReader class


Method Description
int read() It is used to return a character in ASCII form. It returns -1 at the end of file.
void close() It is used to close the FileReader class.

Java FileReader Example


 In this example, we are reading the data from the text file testout.txt using Java FileReader class.
import java.io.FileReader;
public class FileReaderExample
{
public static void main(String args[])throws Exception
{
FileReader fr=new FileReader("D:\\testout.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
}
}

Java FileWriter Class


 Java FileWriter class is used to write character-oriented data to a file. It is character-oriented class
which is used for file handling in java.

 Unlike FileOutputStream class, you don't need to convert string into byte array because it provides
method to write string directly.

Java FileWriter class declaration


Let's see the declaration for Java.io.FileWriter class:
public class FileWriter extends OutputStreamWriter

Methods of FileWriter class


Method Description
void write(String text) It is used to write the string into FileWriter.
void write(char c) It is used to write the char into FileWriter.
void write(char[] c) It is used to write char array into FileWriter.
void flush() It is used to flushes the data of FileWriter.
void close() It is used to close the FileWriter.

Java FileWriter Example


In this example, we are writing the data in the file testout.txt using Java FileWriter class.

import java.io.FileWriter;
public class FileWriterExample
{
public static void main(String args[])
{
try
{
FileWriter fw=new FileWriter("a1.txt");
fw.write("Welcome to java.");
fw.close();
}
catch(Exception e)
{
System.out.println(e); }
System.out.println("Success...");
}}