19
Data Stream “Stream is a process to read a data from 1 resource or send a data to another location.”

Chapter 2.1 : Data Stream

Embed Size (px)

Citation preview

Data Stream “Stream is a process to read a data from 1 resource or send

a data to another location.”

What is Stream

• All data in JAVA platform was read and write by Stream

• Stream is a process to read a data from 1 resource and send a data to another location.

• Example : – Read and write file at and to Harddisk

– Read and write text to console

– Read and write to network

• Stream has 2 type : – Stream Byte (8bit): File for binary data

– Stream Character (16bit) : File for Unicode data

Character Stream

Byte Stream

Java.io Hierarchy

• Package java.io was develop to make easy for user to do an various type for input or output process such as Objects, Strings and Bytes.

• It also design in order to receive input and output from various source such as keyboard, network and disk.

I/O Stream • To READ, a program will open STREAM at a resource

(file, memory, socket) and READ an information in sequential.

• And to WRITE, a program will send an information to a destination. It will open a STREAM and it will WRITE an information in sequential

• hhhhhhhhhhhhh

Reader and Writer

• Use for READ and WRITE text data

• It more easy then InputStream and OutputStream

• For fast performance, use a buffering facility, Example : BufferedReader.

Reader

• To READ a text by paragraph, use below technique :

– File f = new Fine (“C:\\OOP\\MyText.txt”);

– FileReader fr = mew FileReader(f);

– BufferedReader fin = new BufferedReader(fr);

• FileReader is a lane that provide to read a File.

• BufferedReader wrap a FileReader in order to make input process using buffering technique.

Input DataStream

Input DataStream

Reader vs Writer

• Reader

– BufferedReader

– CharArrayReader

– FilterReader

– InputStreamReader

– FileReader

– PipedReader

– StringReader

• Writer – BufferedWriter

– CharArrayWriter

– FilterWriter

– OutputStreamWriter

– FileWriter

– PipedWriter

– StringWriter

– PrintWriter

Reader

• After BufferedReader formed, String will be taken with a readLine() method that use to return a String value.

• To display a data it will use print() or println().

Writer

• Use to WRITE a text line by line use following technique :

– File f = new File (“C:\\OOP\\MyText.txt”);

– FileWriter fw = new FileWriter(f);

– PrintWriter fin = new PrintWriter(fw);

• A file will be process to be FileWriter that is needed for output.

• PrintWriter is needed in order to make program can use a print() and println() method.

Output Stream

Output DataStream

Stream Byte

• Actually stream byte divide by 2 class : InputStream and OutputStream

• InputStream and OutStream is a higher level in java.io package.

Stream Byte

• InputStream and OutStream is a class for stream I/O that use to READ and WRITE a data from and to file.

• For READ a file, we use : FileInputStream

• For WRITE a file, we use : FileOutputStream

InputStream vs OutputStream

How to use a byte stream?

INPUT STREAM OUTPUT STREAM

Create object that oriented with source data. Eg : FileInputStream

Create object that oriented with destination data. Eg : BufferedWriter

Read information from stream by using object method. Eg : read()

Write an information to stream by using object method. Eg : write()

When complete, close method will be call.

When complete, closure method will be call.