0% found this document useful (0 votes)
2 views5 pages

Mod 7

The document provides an overview of Java I/O streams, detailing their importance in reading and writing data in Java applications. It explains the classification of streams into input and output types, as well as byte and character streams, and lists key classes for handling these operations. Additionally, it covers reading console input and writing console output, along with file handling techniques using various Java classes.

Uploaded by

mandalipsita2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Mod 7

The document provides an overview of Java I/O streams, detailing their importance in reading and writing data in Java applications. It explains the classification of streams into input and output types, as well as byte and character streams, and lists key classes for handling these operations. Additionally, it covers reading console input and writing console output, along with file handling techniques using various Java classes.

Uploaded by

mandalipsita2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Web Technology - Module VII

Java I/O Stream


Java I/O (Input and Output) is a mechanism in Java that allows the reading and writing of data.

L
It is an essential part of Java programming, as it facilitates data handling in Java applications,
such as reading user input, writing data to files, and processing streams of data. Java provides
multiple classes and interfaces for handling I/O through streams.

A
A stream is a sequence of data. In Java, a stream is composed of bytes. It's called a stream
because it is like a stream of water that continues to flow.

D
I/O Basics

● Streams: A stream is a sequence of data. In Java, streams are classified into two main

N
types:
○ Input Stream: Used for reading data.
○ Output Stream: Used for writing data.

A
Java I/O Classes: The java.io package contains all the classes required for input and
output operations in Java. Some of the main classes include:
○ InputStream, OutputStream for byte streams.
M
○ Reader, Writer for character streams.
● Types of Streams:
○ Byte Streams: Handle data in bytes (8-bit), suitable for binary data.
○ Character Streams: Handle data in characters (16-bit Unicode), suitable for text
data.
B

Byte Stream

● Byte streams are used to handle raw binary data. They read and write data in bytes,
LA

making them suitable for all kinds of I/O operations that do not involve character data,
such as reading and writing images or binary files.
● Classes in Byte Streams:
○ InputStream: Abstract class representing input byte streams. Common
subclasses:
■ FileInputStream: Reads data from files.
IP

■ BufferedInputStream: Adds buffering capability, improving efficiency.


■ ByteArrayInputStream: Reads data from a byte array.
B
InputStream Hierarchy

L
A
D
N
○ OutputStream: Abstract class representing output byte streams. Common
subclasses:
■ FileOutputStream: Writes data to files.
■ BufferedOutputStream: Adds buffering capability.

A
ByteArrayOutputStream: Writes data to a byte array.

OutputStream Hierarchy
M
B
LA

● Example:
IP
B
Character Stream

● Character streams are used to handle character data, which makes them suitable for
handling text files. They are based on Reader and Writer classes and are used when
the data is in the form of characters (Unicode).

L
● Classes in Character Streams:
○ Reader: Abstract class for reading character streams. Common subclasses:
■ FileReader: Reads data from files.

A
■ BufferedReader: Adds buffering and supports line-by-line reading.
■ StringReader: Reads data from a string.
○ Writer: Abstract class for writing character streams. Common subclasses:

D
■ FileWriter: Writes data to files.
■ BufferedWriter: Adds buffering and supports line-by-line writing.
■ StringWriter: Writes data to a string buffer.

N
● Example:

A
M
B

Reading Console Input


LA

● Java provides classes like InputStreamReader and BufferedReader to read input


from the console.
● System.in is used for reading byte-based input, and can be wrapped in a
BufferedReader to handle character-based input from the console.
● Scanner class:
● Used to read input from the console.
IP

● Methods:
○ nextLine(): Reads an entire line of input.
○ nextInt(): Reads an integer.
○ nextDouble(): Reads a double.
B

○ next(): Reads the next token (word).


● Example:

L
A
Writing Console Output

D
● System.out is an instance of PrintStream and is used to print data to the console.
● It provides various methods like print(), println(), and printf() for formatted
output.

N
● Example:

A
M
Reading and Writing Files

● Java provides classes like FileReader, FileWriter, FileInputStream, and


FileOutputStream for handling files.
● FileReader and FileWriter: Used for reading and writing text files.
B

● FileInputStream and FileOutputStream: Used for reading and writing binary files.
● BufferedReader and BufferedWriter: Enhance file I/O by adding buffering capabilities,
reducing the number of I/O operations, and improving performance.
LA

● Example of Reading a File:


IP
B
● Example of Writing to a File:

L
A
D
N
A
M
B
LA
IP
B

You might also like