You are on page 1of 13

TK1114

FILE OPERATION AND HANDLING


Learning Objective
• Introduction
• File Operation
• Handling Exceptions
Introduction
• Package java.io is used to perform the input and output in Java
• Stream represent input source and output destination
• Stream: sequence of data
• Two kinds of stream: InPutStream & OutPutStream
• File handling allows user to read, write, delete, create and getting data to and
from a file
Stream
• Stream is a series of data

Input stream • Byte stream: involved with byte data


classes (perform on 8-bit)
Byte Stream • FileInputStream
• FileOutputStream
Output stream
classes • Character stream: involved with character
data (perform on 16 bits of Unicode)
Reader classes • FileReader
Character • FileWriter
Stream

Writer classes
When to use…
• Character stream over Byte stream?
• Suitable when we want to process the text files
• Byte stream over Character stream?
• Suitable when we want to process raw data like binary files

• Usually, we used character stream because it is human readable.


• Class names of Character streams end with Reader/Writer while class names of
Byte streams end with InputStream/OutputStream
Operations on File in Java
1. Creating a file
2. Getting information about a file
3. Writing in a file
4. Reading from a file
1. Creating a File
• File class provides method createNewFile() to create a new file on a specified
directory
Create an object of File class, and
assign the location and file name
File fileName = new File("FileClass.txt");
if(fileName.createNewFile())
System.out.println("The file is created"); Method createNewFile is
called to check whether
else
the file is created or not
System.out.println("The file is existed");

• This method returns a Boolean value based on the creation of the file
2. Getting information about file
Method Return type Description
canRead() Boolean To check whether we can read the data of the file or not.
createNewFile() Boolean To create a new empty file.
canWrite() Boolean To check whether we can write the data into the file or not.
exists() Boolean To check whether the specified file is present or not.
delete() Boolean To delete a file.
getName() String To find the file name.
getAbsolutePath() String To get the absolute pathname of the file.
length() Long To get the size of the file in bytes.
list() String[] To get an array of the files available in the directory.
mkdir() Boolean To create a new directory.
Writing on file
• Writer class is superclass that writes the stream of characters
• In order to use the functionality of Writer, we use its subclasses:
• BufferedWriter - similar to FileWriter but it uses internal buffer to write data into file. It is use when
the number of write operations are large. For example writing a 1000 names, it is preferable to use
BufferedWriter than FileWriter
• FileOutputStream - to write raw stream data (byte). Useful for image processing
• FileWriter - the simplest way to write a file. It provides write method to write int, byte array and
String to the file. It writes directly into the file and only be used when the number of writes is less
• PrintWriter - converts primitive datatype into the text format and then writes to the file.

Writer

BufferedWriter FileOutputStream FileWriter PrintWriter


4. Reading from a file
• Reader class is superclass that reads the stream of characters
• In order to use the functionality of Reader, we use its subclasses:
• BufferedReader
• FileInputStream
• FileReader

Reader

BufferedReader FileInputStream FileReader


InputStream vs Reader vs BufferReader
• InputStream/OutputStream
• for byte data
• Reader/Writer
• for character data
• Buffer
• similar to Reader/Writer
• the difference is it pre-fetches
characters in to a buffer for quick
access.
• Useful if there are lots of things to
write/read
Handling Exceptions
• Exception – when an unexpected event occurs (error, file not existed) then the
program will throws an Exception
• In other words, when the exception is throw, the program could not continue until
this unexpected event is solved
1. public static void main(String[] args) throws IOException {…}

2. try and catch block


Example
• Read and processing integer file and write to another file
• Read and write string file

You might also like