File Handling in Java
File Handling in Java
Presenter:
Muhammad Asim Ali Raza
Agenda
• Define data streams
• Explain the InputStream and OutputStream
classes
• Describe the BufferedInputStream and
BufferedOutputStream classes
• Describe Character stream classes
• Define Serialization and describe the need and
purpose of Serialization
Stream Classes
• Input and Output streams are abstract classes and are used
for reading and writing of unstructured sequence of bytes.
• For reading or writing bytes, a subclass of the InputStream
or OutputStream class has to be used respectively.
File Class
• File class directly works with files and the file system.
• A pathname can be absolute or relative.
• In an absolute pathname, no other information is required in order to
locate the required file as the pathname is complete.
• In a relative pathname, information is gathered from some other
pathname.
• A File object uses this path to locate a file.
• The constructors of the File class are as follows:
– File(String dirpath)
– File(String parent, String child)
– File(File fileobj, String filename)
– File(URL urlobj)
Methods of File Class
• The methods in File class help to manipulate the file on the file system.
• Some of the methods in the File class are:
– renameTo(File newname): Names the existing File object with the new name
specified by the variable newname.
– delete(): Deletes the file represented by the abstract path name.
– exists(): Tests the existence of file or directory denoted by this abstract pathname.
– getPath(): Converts the abstract pathname into a pathname string.
– isFile(): Checks whether the file denoted by this abstract pathname is a normal file.
– mkdir(): Creates the directory named by this abstract pathname.
– toPath(): Returns a java.nio.file.Path object constructed from the abstract
path.
. . .
File fileObj = new File(“C:/Java/Hello.txt”);
System.out.println(“Path is: “ +fileObj.getPath());
System.out.println(“Name is: “ +fileObj.getName());
System.out.println(“File exists is: “ +fileObj.exists());
System.out.println(“File is: “ +fileObj.isFile());
...
DataInput Interface and DataOutput Interface
• Data stream supports input/output of primitive data types and
string values. The data streams implement DataInput or
DataOutput interface.
• The DataInput interface has methods for:
– Reading bytes from a binary stream and convert the data to any of the Java
primitive types.
– Converting data from Java modified Unicode Transmission Format (UTF)-8
format into string form.
• The DataOutput interface has methods for:
– Converting data present in Java primitive type into a series of bytes and write
them onto a binary stream.
– Converting string data into Java-modified UTF-8 format and write it into a
stream.
java.io Package
The following figure illustrates that a program uses an output stream to write
data to a destination:
java.io Package
The following Code Snippet displays the working of byte streams using the
FileInputStream class and FileOutputStream class:
Code Snippet
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ByteStreamApp {
public static void main(String[] args) throws IOException {
FileInputStream inObj = null;
FileOutputStream outObj = null;
try {
inObj = new FileInputStream(“c:/java/hello.txt”);
outObj = new FileOutputStream(“outagain.txt”);
int ch;
while ((ch = inObj.read()) != -1) {
outObj.write(ch);
java.io Package [4-7]
}
} finally {
if (inObj != null) {
inObj.close();
}
if (outObj != null) {
outObj.close();
}
}
}
}
• A program that uses character streams adapts to the local character set and
is ready for internationalization.
• All character stream classes are derived from the Reader and Writer
class.
• There are character stream classes that specialize in file I/O operations such
as FileReader and FileWriter.
• The following Code Snippet displays the reading and writing of character
streams using the FileReader and FileWriter class:
Code Snippet
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CharStreamApp {
public static void main(String[] args) throws IOException {
FileReader inObjStream = null;
FileWriter outObjStream = null;
java.io Package
try {
inObjStream = new FileReader(“c:/java/hello.txt”);
outObjStream = new FileWriter(“charoutputagain.txt”);
int ch;
while ((ch = inObjStream.read()) != -1) {
outObjStream.write(ch);
}
} finally {
if (inObjStream != null) {
inObjStream.close();
}
}
}
. . .
Point pointObj = new Point(50,75);
FileOutputStream fObj = new FileOutputStream(“point”);
ObjectOutputStream oos = new ObjectOutputStream(fObj);
oos.writeObject(pointObj);
oos.writeObject(new Date());
oos.close();
. . .
ObjectInputStream Class
. . .
FileInputStream fObj = new FileInputStream(“point”);
ObjectInputStream ois = new ObjectInputStream(fObj);
Point obj = (Point) ois.readObject();
ois.close();
import java.io.Serializable;
public class Employee implements Serializable{
String lastName;
String firstName;
double sal;
}
public class BranchEmpProcessor {
public static void main(String[] args) {
FileInputStream fIn = null;
FileOutputStream fOut = null;
ObjectInputStream oIn = null;
ObjectInputStream Class
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
System.out.println(“finally”);
}
}
}
Summary