SlideShare a Scribd company logo
1 of 18
STREAMS AND INPUT / OUTPUT
FILES
Presentation By: Shehrevar Davierwala
Visit: http://www.authorstream.com/shehrevard
http://www.slideshare.net/shehrevard
http://sites.google.com/sites/techwizardin
1
INTRODUCTION
 So far we have used variables and arrays for
storing data inside the programs. This approach
poses the following limitations:
 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.
 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. 2
CompiledBy:ShehrevarDavierwla
FILE PROCESSING
 Storing and manipulating data using files
is known as file processing.
 Reading/Writing of data in a file can be
performed at the level of bytes,
characters, or fields depending on
application requirements.
 Java also provides capabilities to read and
write class objects directly. The process of
reading and writing objects is called object
serialisation. 3
CompiledBy:ShehrevarDavierwla
C INPUT/OUTPUT REVISION
FILE* fp;
fp = fopen(“In.file”, “rw”);
fscanf(fp, ……);
frpintf(fp, …..);
fread(………, fp);
fwrite(……….., fp);
4
CompiledBy:ShehrevarDavierwla
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
– a sequence of bytes and
characters and support to the
sequential access to the data.
5
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).
6
STREAM TYPES
7
 The concepts of
sending data from one
stream to another
(like a pipe feeding
into another pipe) has
made streams
powerful tool for file
processing.
 Connecting streams
can also act as filters.
 Streams are classified
into two basic types:
 Input Steam
 Output Stream
Source Program
Input Stream
reads
SourceProgram
Output Stream
writes
CompiledBy:ShehrevarDavierwla
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 8
CompiledBy:ShehrevarDavierwla
STREAMS
9
Byte Streams Character streams
Operated on 8 bit (1
byte) data.
Operates on 16-bit
(2 byte) unicode
characters.
Input streams/Output
streams
Readers/ Writers
CompiledBy:ShehrevarDavierwla
CLASSIFICATION OF JAVA
STREAM CLASSES
10
Byte Stream
classes
Character Stream
classes
CompiledBy:ShehrevarDavierwla
11
BYTE INPUT STREAMS
InputStream
ObjectInputStream
SequenceInputStream
ByteArrayInputStream
PipedInputStream
FilterInputStream
PushbackInputStream
DataInputStream
BufferedInputStream
CompiledBy:ShehrevarDavierwla
BYTE INPUT STREAMS - OPERATIONS
public abstract int read() Reads a byte and returns as a
integer 0-255
public int read(byte[] buf, int
offset, int count)
Reads and stores the bytes in buf
starting at offset. Count is the
maximum read.
public int read(byte[] buf) Same as previous offset=0 and
length=buf.length()
public long skip(long count) Skips count bytes.
public int available() Returns the number of bytes that
can be read.
public void close() Closes stream
12
BYTE INPUT STREAM - EXAMPLE
 Count total number of bytes in the file
13
import java.io.*;
class CountBytes {
public static void main(String[] args)
throws FileNotFoundException, IOException
{
FileInputStream in;
in = new FileInputStream(“InFile.txt”);
int total = 0;
while (in.read() != -1)
total++;
System.out.println(total + “ bytes”);
}
}
CompiledBy:ShehrevarDavierwla
WHAT HAPPENS IF THE FILE
DID NOT EXIST
 JVM throws exception and terminates the
program since there is no exception handler
defined.
[add@mundroo] Streams [1:165] java CountBytes
Exception in thread "main"
java.io.FileNotFoundException: FileIn.txt (No
such file or directory)
at java.io.FileInputStream.open(Native
Method)
at
java.io.FileInputStream.<init>(FileInputStream.j
ava:64)
at CountBytes.main(CountBytes.java:12) 14
CompiledBy:ShehrevarDavierwla
15
BYTE OUTPUT STREAMS
OutputStream
ObjectOutputStream
SequenceOutputStream
ByteArrayOutputStream
PipedOutputStream
FilterOutputStream
PrintStream
DataOutputStream
BufferedOutputStream
CompiledBy:ShehrevarDavierwla
BYTE OUTPUT STREAMS - OPERATIONS
public abstract void write(int b) Write b as bytes.
public void write(byte[] buf, int
offset, int count)
Write count bytes starting from
offset in buf.
public void write(byte[] buf) Same as previous offset=0 and
count = buf.length()
public void flush() Flushes the stream.
public void close() Closes stream
16
BYTE OUTPUT STREAM - EXAMPLE
 Read from standard in and write to standard out
17
import java.io.*;
class ReadWrite {
public static void main(string[] args)
throws IOException
{
int b;
while (( b = System.in.read()) != -1)
{
System.out.write(b);
}
}
CompiledBy:ShehrevarDavierwla
SUMMARY
 Streams provide uniform interface for managing
I/O operations in Java irrespective of device
types.
 Java supports classes for handling Input Steams
and Output steams via java.io package.
 Exceptions supports handling of errors and their
propagation during file operations.
18
CompiledBy:ShehrevarDavierwla

More Related Content

What's hot

Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in javaJayasankarPR2
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
 
File handling in vb.net
File handling in vb.netFile handling in vb.net
File handling in vb.netEverywhere
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52myrajendra
 
32sql server
32sql server32sql server
32sql serverSireesh K
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams Ahmed Farag
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1sotlsoc
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Outputphanleson
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing filesMukesh Tekwani
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streamsHaresh Jaiswal
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and OutputEduardo Bergavera
 

What's hot (20)

Basic i/o & file handling in java
Basic i/o & file handling in javaBasic i/o & file handling in java
Basic i/o & file handling in java
 
File Handling
File HandlingFile Handling
File Handling
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Java IO
Java IOJava IO
Java IO
 
Basic of java
Basic of javaBasic of java
Basic of java
 
Filehandling
FilehandlingFilehandling
Filehandling
 
File handling in vb.net
File handling in vb.netFile handling in vb.net
File handling in vb.net
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
 
Javaio
JavaioJavaio
Javaio
 
Stream
StreamStream
Stream
 
32sql server
32sql server32sql server
32sql server
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
Chapter 10.1
Chapter 10.1Chapter 10.1
Chapter 10.1
 
30csharp
30csharp30csharp
30csharp
 
OSCh11
OSCh11OSCh11
OSCh11
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
 
Python reading and writing files
Python reading and writing filesPython reading and writing files
Python reading and writing files
 
File handling
File handlingFile handling
File handling
 
08. handling file streams
08. handling file streams08. handling file streams
08. handling file streams
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 

Similar to Jstreams (20)

File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
Javaio
JavaioJavaio
Javaio
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
File Management and manipulation in C++ Programming
File Management and manipulation in C++ ProgrammingFile Management and manipulation in C++ Programming
File Management and manipulation in C++ Programming
 
Java programming Chapter 4.pptx
Java programming Chapter 4.pptxJava programming Chapter 4.pptx
Java programming Chapter 4.pptx
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
Unit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxUnit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptx
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
JAVA
JAVAJAVA
JAVA
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Data file handling
Data file handlingData file handling
Data file handling
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
7 Data File Handling
7 Data File Handling7 Data File Handling
7 Data File Handling
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9Filepointers1 1215104829397318-9
Filepointers1 1215104829397318-9
 
UNIT 5.pptx
UNIT 5.pptxUNIT 5.pptx
UNIT 5.pptx
 

More from Shehrevar Davierwala

More from Shehrevar Davierwala (20)

Introduction_Swift
Introduction_SwiftIntroduction_Swift
Introduction_Swift
 
PsudoCode.pptx
PsudoCode.pptxPsudoCode.pptx
PsudoCode.pptx
 
Number System.pptx
Number System.pptxNumber System.pptx
Number System.pptx
 
Java Script (Module 1).pptx
Java Script (Module 1).pptxJava Script (Module 1).pptx
Java Script (Module 1).pptx
 
Website in Clicks Day 2
Website in Clicks Day 2Website in Clicks Day 2
Website in Clicks Day 2
 
Develop Website in Clicks
Develop Website in ClicksDevelop Website in Clicks
Develop Website in Clicks
 
Build Virtual Assistant Using AI
Build Virtual Assistant Using AI Build Virtual Assistant Using AI
Build Virtual Assistant Using AI
 
Build brand reputation using facebook
Build brand reputation using facebookBuild brand reputation using facebook
Build brand reputation using facebook
 
Digital Marketing Session 2
Digital Marketing Session 2Digital Marketing Session 2
Digital Marketing Session 2
 
Learn Digital Marketing : 0 to Hero Day 1
Learn Digital Marketing :  0 to Hero Day 1 Learn Digital Marketing :  0 to Hero Day 1
Learn Digital Marketing : 0 to Hero Day 1
 
Standard template
Standard templateStandard template
Standard template
 
Digital Marketing for Sustainable Business - Afghan Perspective
Digital Marketing for Sustainable Business - Afghan Perspective  Digital Marketing for Sustainable Business - Afghan Perspective
Digital Marketing for Sustainable Business - Afghan Perspective
 
Developing stunning website in clicks - 2
Developing stunning website in clicks - 2Developing stunning website in clicks - 2
Developing stunning website in clicks - 2
 
Developing stunning website in clicks
Developing stunning website in clicksDeveloping stunning website in clicks
Developing stunning website in clicks
 
Google forms for data analysis
Google forms for data analysisGoogle forms for data analysis
Google forms for data analysis
 
Webdesign session1
Webdesign session1Webdesign session1
Webdesign session1
 
Tech talk webtech
Tech talk webtechTech talk webtech
Tech talk webtech
 
Tech talk php_cms
Tech talk php_cmsTech talk php_cms
Tech talk php_cms
 
Ph pbasics
Ph pbasicsPh pbasics
Ph pbasics
 
Php mysql
Php mysqlPhp mysql
Php mysql
 

Recently uploaded

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleCeline George
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 

Recently uploaded (20)

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Multi Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP ModuleMulti Domain Alias In the Odoo 17 ERP Module
Multi Domain Alias In the Odoo 17 ERP Module
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 

Jstreams

  • 1. STREAMS AND INPUT / OUTPUT FILES Presentation By: Shehrevar Davierwala Visit: http://www.authorstream.com/shehrevard http://www.slideshare.net/shehrevard http://sites.google.com/sites/techwizardin 1
  • 2. INTRODUCTION  So far we have used variables and arrays for storing data inside the programs. This approach poses the following limitations:  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.  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. 2 CompiledBy:ShehrevarDavierwla
  • 3. FILE PROCESSING  Storing and manipulating data using files is known as file processing.  Reading/Writing of data in a file can be performed at the level of bytes, characters, or fields depending on application requirements.  Java also provides capabilities to read and write class objects directly. The process of reading and writing objects is called object serialisation. 3 CompiledBy:ShehrevarDavierwla
  • 4. C INPUT/OUTPUT REVISION FILE* fp; fp = fopen(“In.file”, “rw”); fscanf(fp, ……); frpintf(fp, …..); fread(………, fp); fwrite(……….., fp); 4 CompiledBy:ShehrevarDavierwla
  • 5. 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 – a sequence of bytes and characters and support to the sequential access to the data. 5
  • 6. 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). 6
  • 7. STREAM TYPES 7  The concepts of sending data from one stream to another (like a pipe feeding into another pipe) has made streams powerful tool for file processing.  Connecting streams can also act as filters.  Streams are classified into two basic types:  Input Steam  Output Stream Source Program Input Stream reads SourceProgram Output Stream writes CompiledBy:ShehrevarDavierwla
  • 8. 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 8 CompiledBy:ShehrevarDavierwla
  • 9. STREAMS 9 Byte Streams Character streams Operated on 8 bit (1 byte) data. Operates on 16-bit (2 byte) unicode characters. Input streams/Output streams Readers/ Writers CompiledBy:ShehrevarDavierwla
  • 10. CLASSIFICATION OF JAVA STREAM CLASSES 10 Byte Stream classes Character Stream classes CompiledBy:ShehrevarDavierwla
  • 12. BYTE INPUT STREAMS - OPERATIONS public abstract int read() Reads a byte and returns as a integer 0-255 public int read(byte[] buf, int offset, int count) Reads and stores the bytes in buf starting at offset. Count is the maximum read. public int read(byte[] buf) Same as previous offset=0 and length=buf.length() public long skip(long count) Skips count bytes. public int available() Returns the number of bytes that can be read. public void close() Closes stream 12
  • 13. BYTE INPUT STREAM - EXAMPLE  Count total number of bytes in the file 13 import java.io.*; class CountBytes { public static void main(String[] args) throws FileNotFoundException, IOException { FileInputStream in; in = new FileInputStream(“InFile.txt”); int total = 0; while (in.read() != -1) total++; System.out.println(total + “ bytes”); } } CompiledBy:ShehrevarDavierwla
  • 14. WHAT HAPPENS IF THE FILE DID NOT EXIST  JVM throws exception and terminates the program since there is no exception handler defined. [add@mundroo] Streams [1:165] java CountBytes Exception in thread "main" java.io.FileNotFoundException: FileIn.txt (No such file or directory) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(FileInputStream.j ava:64) at CountBytes.main(CountBytes.java:12) 14 CompiledBy:ShehrevarDavierwla
  • 16. BYTE OUTPUT STREAMS - OPERATIONS public abstract void write(int b) Write b as bytes. public void write(byte[] buf, int offset, int count) Write count bytes starting from offset in buf. public void write(byte[] buf) Same as previous offset=0 and count = buf.length() public void flush() Flushes the stream. public void close() Closes stream 16
  • 17. BYTE OUTPUT STREAM - EXAMPLE  Read from standard in and write to standard out 17 import java.io.*; class ReadWrite { public static void main(string[] args) throws IOException { int b; while (( b = System.in.read()) != -1) { System.out.write(b); } } CompiledBy:ShehrevarDavierwla
  • 18. SUMMARY  Streams provide uniform interface for managing I/O operations in Java irrespective of device types.  Java supports classes for handling Input Steams and Output steams via java.io package.  Exceptions supports handling of errors and their propagation during file operations. 18 CompiledBy:ShehrevarDavierwla