14
BYTE STREAM AND CHARACTER STREAM SHABEER ISMAEEL MSC IT II SEMESTER DEPT. OF INFORMATION TECHNOLOGY

Byte stream and character stream

Embed Size (px)

Citation preview

Page 1: Byte stream and character stream

BYTE STREAM AND CHARACTER STREAM SHABEER ISMAEEL

MSC IT II SEMESTERDEPT. OF INFORMATION TECHNOLOGY

Page 2: Byte stream and character stream

STREAMS

In computer science Streams are defined as a sequence of data elements that is made available over time.

It represents a Source (which generates the data in the form of Stream) and a destination(which consumes or read data available as Stream).

In other simple words it is a flow of data from which you can read or write data into it.

Page 3: Byte stream and character stream

Reading Data from a stream

Inserting Data into the stream

Page 4: Byte stream and character stream

To make a stream active we have to use some objects so as to Pull data from a stream or Push data into the stream.

Pull Streams• Pull Streams are used to pull

data or to retrieve data from the Stream.

• We can use this stream by creating an object of them using read() method.

Push Streams• Push Streams are used to

push data or to insert data into the Stream.

• We can use this stream by creating an object of them using write() method.

Page 5: Byte stream and character stream

Basic Streams In Java

Java defines two types of Streams. They are: 1.Byte Streams. 2.Character Streams.

Page 6: Byte stream and character stream

Byte Streams• These are the most basic type of Stream for I/O operations and are used

to process data byte by byte.

• Byte based Streams generally end up calling by word “Stream ” like InputStream(for input operation) and OutputStream(for output operation).

----Now lets have a look at some important classes for Byte Streams…

Page 7: Byte stream and character stream

Stream class Description

BufferedInputStream Used for Buffered Input Stream.

BufferedOutputStream Used for Buffered Output Stream.

DataInputStream Contains method for reading java standard datatype

DataOutputStream An output stream that contain method for writing java standard data type

FileInputStream Input stream that reads from a file

FileOutputStream Output stream that write to a file.

InputStream Abstract class that describe stream input.

OutputStream Abstract class that describe stream output.

PrintStream Output Stream that contain print() and println() method

Page 8: Byte stream and character stream

File Reading and Writing Example using Byte Streams

• To show you the use of Byte Streams here in this example I will take image as an input file so that it could be clear that Byte Streams can be used for all types of files( except Strings or text files).

Page 9: Byte stream and character stream

package codingeekExamples; import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream; public class ByteStreamsExample {    public static void main(String[] args) {         try {        // Declaring and initializing both the Streams inside try so that there        // is no need to close them. You can do this with many other classes but        // make sure that they all implements the Closable Interface                InputStream inputStream = new FileInputStream("input.jpg");                OutputStream outputStream = new FileOutputStream("output.jpg")                } catch(IOException e) { } {            int c;                         //continue reading till the end of the file            while ((c = inputStream.read()) != -1) {                             //writes to the output Stream                outputStream.write(c);            }        }    }}

Page 10: Byte stream and character stream

Character Streams.

• Character Streams are specially designed to read and write data from and to the Streams of Characters.

• We require this specialized Stream because of different file encoding systems.

• Character based Streams generally end up calling by word “Reader ” or “Writer” like InputStreamReader(for reading operation) and OutputStreamReader(for writing operation).

Page 11: Byte stream and character stream

---Now lets have a look at some important classes for Character Streams…Stream class Description

BufferedReader Handles buffered input stream.

BufferedWriter Handles buffered output stream.

FileReader Input stream that reads from file. (Not recommended)

FileWriter Output stream that writes to file. (Not recommended)

InputStreamReader Input stream that translate byte to character (Recommended)

OutputStreamReader Output stream that translate character to byte. (Recommended)

PrintWriter Output Stream that contain print() and println() method.

Reader Abstract class that define character stream input

Writer Abstract class that define character stream output

Page 12: Byte stream and character stream

File Reading and Writing Example using Character Streams

• You can read any text file using this program but ensure that the file is in classpath or provide a fully qualified pathname of the file.

• In this example we will use InputStreamReader and OutputStreamReader instead of FileReader and FileWriter because…

---- InputStreamReader and OutputStreamWriter are basically a bridge between Byte Stream and Character Stream and hence can process any type of input stream like network connections, zip files etc. including files.

Page 13: Byte stream and character stream

package codingeekExamples; import java.io.*;import java.nio.charset.UnsupportedCharsetException; public class CharacterStreamExample {     public static void main(String[] args) {         try {                      Reader reader = new InputStreamReader(new FileInputStream("input.txt"), "UTF-8");                Writer writer = new OutputStreamWriter(new FileOutputStream("output.txt"), "UTF-8");               int c;             // Reading and Writing single character at a time until end of file            // is reached            while ((c = reader.read()) != -1) {                writer.write(c);            }            System.out.println("File Written successfully");                         // Handling exceptions thrown by Streams to ensure that our program            // ends in a user friendly way        } catch (FileNotFoundException e) {            System.out.println("File not found");        } catch (UnsupportedCharsetException e) {            System.out.println("Given charset is unsupported");        }    }}

Page 14: Byte stream and character stream