STD 12 Computer Chapter 11 File Handling in Java
STD 12 Computer Chapter 11 File Handling in Java
COMPUTER FILES
Storage device of computer system can be broadly classified into two categories:
o Volatile storage and non-volatile storage
Volatile storage is temporary.
o Value stored in variables is lost when a computer is shutdown.
o A java program that stores a value in a value in a variable name uses
Random Access Memory (RAM). Apart from variables, objects and their
reference are generally storedin RAM, once th program terminates or the
computer shuts down, the data is lost.
Non-Volatile storage is a permanent storage.
o Data is not lost when a computer loses power
o A computer file is a collection of data stored on non-volatile device. Files
exist on permanent storage devices such as hard disks, USB drives,
optical disks and compactdiscs. Data stored in such files is often called
persistence data.
Files can be classified broadly into two categories:
o Text Files and binary files
Text files can be data files that contain facts, such as a payroll file that contains
employee numbers, names and salaries; or some text files can be program files or
application files thatstore software instructions.
o Text files contains data that can be read in a text editor because the
data has beenencoded using a scheme such as ASCII or Unicode.
o Files created through editors like gedit, vi, pico are example of text
files. They mayhave extensions like txt, java or c.
Binary files contain data that has not been encoded as text. Their content are in
binaryformat, which means the data is accessed in terms of bytes.
o Some example of binary files are jpeg, mp3 and class.
Java language supports various operations that can be performed on file or on
directories.
Few operations that can be performed on files using java programs are listed below:
Determining the path of a file or a directory opening a file
Writing in a file Reading from a file
Closing a file deleting a file
Querying the attribute of a file
Java provides built-in classes that contain methods to help us with these tasks that is
related toFile. These classes are present in java.io package.
Java uses the concepts of streams and it provides two different categories of java
classes to perform I/O operations on bytes and character
INTRODUCTION TO STREAMS
Java uses stream classes to carry out read and write operations on files.
Hard disk can be classified as both input and output device as we can store and read data from the
Presented by Nuzhat Ibrahim Memon 2
files. There are various devices available in the market from different manufacturers and different
capabilities. For example, hard disk are manufactured by various companies and come with different
storage capacities like 500 GB or 1 Tb. Apart from this, hard disk can be connected using different
cables like USB or SATA.
A java programmer does not need to worry about the technical details like type of hard disk
and its capacity while developing a program to perform read/write operations over the files.
This ispossible because java language provides functionality of streams.
Stream is an abstract representation of an input or output device that is used as a source
or destination for data.
We can visualize a stream as a sequence of bytes that flows into the program or that flows out of
our program.
We can write data or read data using streams.
When we write data to stream, the stream is called an output stream. The output stream
can transfer data from the program to a file on a hard disk or a monitor or to some other
computer over the network.
An input stream is used to read data from an external device to the program; it can
transfer data from keyboard or from the file on a hard disk to the program.
The main reason for using streams for input or output operations is to make our program
independent of the devices involved. Two advantages of streams are:
• Programmer does not need to worry about the technical details of the device.
• The program can work for a variety of input/output devices without any changes to the source code.
Character stream classes can be further classified into Reader and Writer classes
Reader Writer
FileReader Fiiewriter
WRITER CLASSES
Writer class is the base class for writing a character stream.
The abstract Writer class defines the functionality that is available for all character output streams.
FileWriter class is used in our program to perform write operations.
Writer class is abstract so its used by its subclass.
IOException
o Writer class throw IOException when there is a failed I/O operations
o IOEXception is a checked exception, so that we must take care of it. If we do not take care of
IOException it results into compiling error.
The OutputStreamWriter class extends Writer Class. It converts stream of characters to a stream of
bytes.
The FileWriter class extends OutputStreamWriter and outputs characters to a file.
FileWriter(String filepath, Boolean append) throws IOException. In this constructor by true value of
append, characters are appended to the end of file and by false value of append, the exiting
contents of the file are overwritten.
Methods like write() and close() from Writer class
Statement to create a file charfile1.txt by using file object named fwobject:
o FileWriter fwobject=new FileWriter(“charfile1.txt”);
write() mehtod is used to write few lines into the file.
fwobject.close() is used to close object named fwobject.
cat charfile1 is used to display contents of a file charfile1.txt
To close stream object is an important process after writing to a file accomplished.
FileOutputStream class
It is subclass of OutputStream.
It is used to write bytes to the file or some output stream.
To use this class and its method, first we need to create a file object, then we could use the write
method derived from the abstract class OutputStream to write byte into a file.
write() method derived from OutputStream is used to write byte into a file.
5
Presented by Nuzhat Ibrahim Memon
Methods of FileOutputStream class
Methods Description
void close() Closes the file output steam & releases any system resources associated with the stream.
int write() Writes the specified byte to this file output stream.
Int write(byte[] b) Writes b.length bytes of data from the specified byte array to this file output stream.
A constructor of a FileOutputStream class can be accepted as a
o A String containing the path to the file location o Object of the File class
Constructor of FileOutputStream class
o FileOutputStream(String name) throws FileNotFoundException
o FileOutputStream(File file) throws FileNotFoundException
o File fobj=new File(“/java/files/charfile1.txt”);
FileOutputStream fosobj=new FileOutputStream(fobj); OR
FileOutputStream fosobj=new FileOutputStream((“/java/files/charfile1.txt”);
Constructor can throw FileNotFoundException
o If the filename refers to directory ratherthan regular file
o File does not exist
o File cannot be opened for some reason
Extra Points
File refers to a collection of data stored on anonvolatile device in a
computer system.
Smallest to largest piece of data in the datahierarchy is character:field:record:file
More about Stream:
o Streams always flow in two directions, streamsare channels through which the data flow andonly one
stream can be open in a program at a time.
Space is used as a separator between fields of arecord.
Scanner class can be used for performingfollowing operation:
o Accept input from the keyboard
o Read from the file
o Parse s string separated by delimiters
Two categories of storage device::Volatilestorage temporary & Non-volatile
storagePermanent
Two categories of file: Text(character)files &Binary files
30 methods are available in java for a file class and Many classes are there in java to work on
character and byte.
Abstract classes cannot be used to create anobject.
Java.io.Writer and java.io.Reader is known as abstract class and come with the set of methodsto be
implemented by its subclasses. Both created from an object-class.
http://docs.oracle.com/javacse/6/docs/api/ website contain detailed description of methodsand
constructors
FileReader and Scanner both classes are used toread from the file or from the keyboard.
Java file provide classes to store object, to retrieve objects and to access a file randomly.
Java provides classes to perform operations insequentially or to directly jump to nth record.
Reader is used to access data from a file
Scanner Classjava.util
Console Class [hidden password]java.io