Download FILE IO IN JAVA AND ITS DETAIL DESCRIPTION and more Thesis Java Programming in PDF only on Docsity!
Streams and Input/Output Files
Part I
Chapter- 07
Mohammed Husain Bohara
Streams & Files
The objectives of this chapter are:
To understand the principles of I/O
streams and where to use them
To understand the options and limitations
of I/O streams
To become comfortable with the
mechanisms for accessing the file system
Limitation
The data is lost when variable goes out of
scope or when the program terminates.
That is data is stored in temporary/mail
memory is released when program
terminates.
It is difficult to handle large volumes of
data.
Solution
We can overcome this problem by storing
data on secondary storage devices such
as floppy or hard disks.
The data is stored in these devices using
the concept of Files and such data is
often called persistent data.
C Input/Output Revision
FILE* fp;
fp = fopen(“In.file”, “rw”);
fscanf(fp, ……);
frpintf(fp, …..);
fread(………, fp);
fwrite(……….., fp);
Java Application Requirement
abc.txt Java Application xyz.txt
FIS
read
FOS
write
Will it Compile or not
javap java.io.FileInputStream
Output
Data will be copied successfully
What will be the output here?
import java.io.*;
class FileIOExample
{
public static void main(String[] args) throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("abc.txt"); FileOutputStream fos = new FileOutputStream("xyz.txt"); int c; while((c=fis.read())!=-1) { System.out.println(c); fos.write(c); } fis.close(); fos.close(); }
}
I/O and Data Movement
The flow of data into a program
(input) may come from different
devices such as keyboard,
mouse, memory, disk, network,
or another program.
The flow of data out of a
program (output) may go to the
screen, printer, memory, disk,
network, another program.
Both input and output share a
certain common property such as
unidirectional movement of data
characters and support to the
sequential access to the data.
Streams
Java Uses the concept of
Streams to represent the
ordered sequence of data, a
common characteristic shared
by all I/O devices.
Streams presents a uniform,
easy to use, object oriented
interface between the
program and I/O devices.
A stream in Java is a path
along which data flows (like a
river or pipe along which
water flows).
Java Stream Classes
Input/Output related classes are defined in
java.io package.
Input/Output in Java is defined in terms of
streams.
A stream is a sequence of data, of no
particular length.
Java classes can be categorised into two
groups based on the data type one which
they operate:
Byte streams
Character Streams
Streams
Byte Streams Character streams
Operated on 8 bit (
byte) data.
Operates on 16-bit
(2 byte) unicode
characters.
Input
streams/Output
streams
Readers/ Writers