Power Point Presentation on Input/Output Streams in Java

roshphilip19 6 views 35 slides Sep 17, 2025
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

Power Point Presentation on Input/Output Streams in Java


Slide Content

Module 4 INPUT/OUTPUT STREAMS 1

Input/Output in Java 1. I/O in Java Input : Data coming into a program (keyboard input, file reading, network data). Output : Data going out of a program (printing on console, writing into a file, sending over network). Java handles I/O through a stream-based model (sequences of data) 2. Streams in Java A stream is a sequence of data (bytes or characters). Java divides streams into two main categories : (a) Byte Streams Handle raw binary data (images, audio, video, PDF, etc.). Classes: InputStream (abstract class, for input) OutputStream (abstract class, for output) Examples: FileInputStream → read binary files. FileOutputStream → write binary files. 2

Input/Output in Java (b) Character Streams Handle text data (Unicode characters). Classes: Reader (abstract class, for input) Writer (abstract class, for output) Examples: FileReader → read text files. FileWriter → write text files.   3

Byte Stream Classes The two most fundamental classes are: InputStream (abstract class) Superclass for all classes that represent an input stream of bytes. Common subclasses: FileInputStream (read data from a file) ByteArrayInputStream (read from byte array) BufferedInputStream (buffered reading) DataInputStream (read primitive data types). OutputStream (abstract class) Superclass for all classes that represent an output stream of bytes. Common subclasses: FileOutputStream (write data to a file) ByteArrayOutputStream (write into byte array) BufferedOutputStream (buffered writing) DataOutputStream (write primitive data types).   4

Constraints in File Objects The following example creates three files: f1 , f2 , and f3 . The first File object is constructed with a directory path as the only argument. The second includes two arguments—the path and the filename. The third includes the file path assigned to f1 and a filename; f3 refers to the same file as f2 . 5

Basic Methods in I/O Streams 6

Copying File with Byte Stream This program copies input.txt → output.txt byte by byte. 7

Character Streams in Java Character streams are used to read and write text data (characters). They handle 16-bit Unicode characters , making them suitable for internationalization (languages beyond English). Unlike byte streams (which handle raw 8-bit bytes), character streams automatically translate bytes to characters using a character encoding (like UTF-8). Base Classes (in java.io) Reader (abstract class) → For reading characters. Writer (abstract class) → For writing characters. All character stream classes extend from these two. 8

Character Stream Classes For Reading (subclasses of Reader): FileReader → Reads characters from a file. BufferedReader → Reads text efficiently using a buffer; supports readLine (). CharArrayReader → Reads from a character array. InputStreamReader → Converts byte streams to character streams (bridge class). StringReader → Reads from a string. For Writing (subclasses of Writer): FileWriter → Writes characters to a file. BufferedWriter → Writes efficiently with buffering; supports newLine (). CharArrayWriter → Writes to a character array. OutputStreamWriter → Converts character streams to byte streams . StringWriter → Writes into a string buffer. PrintWriter → Prints formatted text (commonly used for console/file output). 9

Character Stream Classes 10

FileReader and FileWriter 11

Low-Level Streams vs High-Level Streams in Java I/O 1. Low-Level Streams These streams are the basic building blocks in Java I/O. They directly interact with the data source (like a file, byte array, or socket). They can open/read/write the file on their own. Examples: FileInputStream (reads raw bytes from a file) FileOutputStream (writes raw bytes to a file) FileReader (reads characters from a file) FileWriter (writes characters to a file) These are self-sufficient . You can use them alone without chaining. 12

Low-Level Streams vs High-Level Streams in Java I/O 2. High-Level Streams These are wrapper streams that add extra functionality on top of low-level streams. They cannot open a file by themselves — instead, they need a low-level stream as input. They depend on low-level streams to do the actual reading/writing. Examples: BufferedInputStream (adds buffering to FileInputStream ) BufferedOutputStream BufferedReader (adds buffering and readLine () method to FileReader ) DataInputStream (lets you read primitives like int, double, etc., from a stream) ObjectInputStream (reads Java objects from a stream) The  LineNumberBuffer  reads from the  BufferedReader  object and allocates line numbers to each line. They decorate or enhance low-level streams, following the Decorator Pattern . 13

Stream Chaining in Java In Java Streams API (introduced in Java 8), stream chaining refers to the process of connecting multiple stream operations together in a single flow (pipeline). Each operation returns a new Stream (except terminal operations), so we can "chain" methods one after another. In Java I/O, stream chaining means combining two or more streams together so that they work in a pipeline. Each stream in the chain has a specific responsibility: Low-level streams handle the actual connection to the data source (like a file, memory, or network). High-level streams sit on top of low-level streams to add extra features such as buffering, data conversion, or formatting. 14

RULES OF CHAINING STREAMS IN JAVA To avoid exceptions and errors we much stick to certain guidelines while chaining of streams in Java. Following rules are to be followed. 1.  The  input for a high-level stream can come from a  low-level stream or another high-level stream . That is, in programming, the  constructor of a high-level stream  can be passed with an  object of low-level or high-level . 2.  Being  low-level , the low-level stream should work by itself. It is  not  entitled to  get passed with any other stream . In programming, the  low-level stream opens the file  and hand it over (passes) to a high-level stream. High-level stream cannot open a file directly. That is,  high-level streams just add extra functionality  and depend solely on low-level streams. 15

Stream Chaining Here the  FileReader  actually  reads  from source file. The  BufferedReader  uses  the FileReader object  to read input from. The  LineNumberBuffer  reads from the  BufferedReader  object and allocates line numbers to each line. 16

Reading and writing to Console Buffered Reader Class: This method is used by wrapping the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader , read input from the user in the command line.  The input is buffered for efficient reading. Output: Enter your city: Chennai You live in Chennai 17

Reading and writing to Console The main purpose of the Scanner class is to parse primitive types and strings using regular expressions; It is also can be used to read input from the user in the command line.  Convenient methods for parsing primitives ( nextInt (), nextFloat (), …) from the tokenized input. Output: Enter a String Hello, How are you? You entered string :Hello, How are you? Enter a Integer 25 You entered integer: 25 Enter a Float 34 You entered float: 34.0 18

Reading and writing to Console It can be used for reading password-like input without echoing the characters entered by the user; The format string syntax can also be used (like System.out.printf ()).  Output: Enter a string: Hello World You entered string is : Hello World 19

Reading and writing to Console Command line argument These inputs are passed to the program during execution and are stored as strings in the args [] array. If we need numeric values, we can convert the strings using methods like Integer.parseInt () or Float.parseFloat (). These programs are run from the command line, with inputs provided while executing the program. javac Geeks.java java Main Hello World 20

Reading and Writing on Files 1. Basics of File I/O in Java I/O = Input/Output → Reading data from a source (like a file, keyboard) and writing data to a destination (like a file, console). In Java, all file operations are handled through java.io and java.nio packages. A file is treated as a stream of data: Input Stream → Reads data from a file. Output Stream → Writes data to a file. 2. Types of Streams There are two broad categories of streams in Java: Byte Streams (binary data, e.g., images, audio, video, etc.) Classes: FileInputStream , FileOutputStream Character Streams (textual data, human-readable text) Classes: FileReader , FileWriter Byte streams handle raw binary data , while character streams handle Unicode characters . 21

Reading from a file (a) Using FileInputStream (Byte Stream) 22

Reading from a file (b) Using FileReader (Character Stream) 23

Reading from a file (c) Using BufferedReader (Efficient Way) 24

Writing to a file (a) Using FileOutputStream (Byte Stream) (b) Using FileWriter (Character Stream) 25

Special Streams in JAVA 26

Special Streams in JAVA In Java, streams are used for input and output operations (I/O). Apart from the basic byte streams ( InputStream , OutputStream ) and character streams (Reader, Writer). Java provides some special-purpose streams to handle advanced I/O operations. These are often called Special Streams . Types of Byte Streams Buffered Streams Data Streams Print Streams Object Streams Sequence Streams LineNumber Streams (Deprecated but conceptual) 27

Special Streams in JAVA Buffered Streams A buffered stream, represented by classes like BufferedInputStream and BufferedOutputStream , enhances the performance of input and output operations by introducing a buffer. This buffer is a temporary memory area where data is stored before being written to or read from the underlying stream ( e.g., a FileInputStream or FileOutputStream ). Classes: BufferedInputStream , BufferedOutputStream , BufferedReader , BufferedWriter Purpose: Improves performance by reducing the number of I/O operations. 28 data.txt Hello Java Welcome to Streams BufferedReader Example output: Hello Java Welcome to Streams BufferedReader Example

Special Streams in JAVA 2. Data Streams Classes: DataInputStream , DataOutputStream Purpose: Allows reading and writing of Java primitive data types (int, float, double, char, etc.) in a machine-independent way. 29 Output: 25 99.99

Special Streams in JAVA 3. Print Streams The PrintStream class in Java, found within the java.io package, is a specialized output stream designed for writing formatted data in a human-readable text format, rather than raw bytes. Class: PrintStream Purpose: Provides convenient methods to print representations of primitive values, objects, and strings. 30 Output: Hello from PrintStream ! Value: 100

Special Streams in JAVA 4. “Object streams" refer to ObjectOutputStream and ObjectInputStream classes, which are part of the java.io package. These streams facilitate the process of serialization and deserialization of Java objects. Serialization is the process of converting a Java object into a sequence of bytes, which can then be written to a file, sent over a network, or stored in a database. Deserialization is the reverse process, where the sequence of bytes is converted back into a live Java object. Classes: ObjectInputStream , ObjectOutputStream Purpose: Used for serialization and deserialization (storing objects as streams and reading them back). 31

Special Streams in JAVA 32 Output: Hello Object Stream

Input Stream Reader The  InputStreamReader  is a bridge between byte streams and character streams in Java. It reads bytes from an input stream and decodes them into characters using a specified charset. Converts bytes to characters : Takes raw bytes from InputStream and converts them to characters Character encoding : Can handle different character encodings (UTF-8, UTF-16, ISO-8859-1, etc.) Wrapper class : Typically wraps around other InputStreams like FileInputStream 33

Input Stream Reader 34 Output: Enter some text (type 'exit' to quit): Hello World You entered: Hello World Java Programming is fun! You entered: Java Programming is fun! Testing 123 You entered: Testing 123 exit

Simple Character-by-Character Reading – UTF 35 Output: Type something and press Enter: Hello Character: H (Unicode: 72) Character: e (Unicode: 101) Character: l (Unicode: 108) Character: l (Unicode: 108) Character: o (Unicode: 111) String: Hello World Int: 42 Double: 3.14
Tags