22
INPUT & OUTPUT STREAM

Topic 2 Part2

Embed Size (px)

Citation preview

Page 1: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 1/22

INPUT & OUTPUT STREAM

Page 2: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 2/22

Course Learning Outcomes :

• Upon completion of this course, the students should be

able to:

1. Describe the features and basic concepts of Java language.

2. Write, compile Java source code and interpret Java byte code

using Java Development Kit (JDK).

3. Implement the key of OOP concepts: classes and objects,

inheritance and polymorphism.

4. Incorporate exception handling in Java Programming.

Page 3: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 3/22

Contents :

• Define input stream (System.in)and output stream(System.out) inJava programs

• Implement input stream(System.in) and output stream(System.out) in Java programs

Explain the use of InputStreamReader andBufferedReader classes

Page 4: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 4/22

STREAM CLASS

• Stream : sequence of data which the length is

undetermined.

• It is called stream because it is like a stream of water that continues to flow.

• All input/output are handled in streams

• Java offer several stream classes which is usefor processing all kinds of data.

Page 5: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 5/22

STREAM CLASS

• It can be categorized in TWO types :

 – Byte stream

• 8 bit streams that only can be understood by machine.

 – Character stream

• 16 bit streams that can be understood by human

Page 6: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 6/22

STREAM CLASS

• Stream: an object that either delivers data to its

destination (screen, file, etc.) or that takes data

from a source (keyboard, file, etc.) – it acts as a buffer between the data source and destination 

• InputStream and OutputStream classes are

the basics of all byte streams

• A stream connects a program to an I/O object

Page 7: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 7/22

STREAM CLASS

• A few classes is used to process the streams :

 – InputStream

• a stream that provides input to a program • To change from (8 bit streams > 16 bit streams)

• System.in connects a program to the keyboard 

 – OutputStream

• a stream that accepts output from a program 

• To change from (16 bit streams > 8 bit streams)

• System.out connects a program to the screen

Page 8: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 8/22

STREAM CLASS

• Reader / Writer is the basics of all character

stream class

• Three predefined stream that can be used : – System.in > to read data bytes entered from the

keyboard

 –

System.out > to display bytes on the screen – System.err > to display error message

Page 9: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 9/22

Method for BufferedReader

• readLine: read a line into a String

• no methods to read numbers directly, so read

numbers as Strings and then convert them

(StringTokenizer later)

• read: read a char at a time

close: close BufferedReader stream

Page 10: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 10/22

Method for BufferedReaderExample:

Reading a File

Name from the

Keyboard

public static void main(String[] args)

{

String fileName = null; // outside try block, can be used in catchtry

{ Scanner keyboard = new Scanner(System.in);

System.out.println("Enter file name:");

fileName = keyboard.next();

BufferedReader inputStream =

new BufferedReader(new FileReader(fileName));

String line = null;line = inputStream.readLine();

System.out.println("The first line in " + filename + " is:");

System.out.println(line);

 // . . . code for reading second line not shown here . . .

inputStream.close();

}

catch(FileNotFoundException e)

{System.out.println("File " + filename + " not found.");

}

catch(IOException e)

{

System.out.println("Error reading from file " + fileName);

}

}10

reading a file name

from the keyboard

closing the file

using the file name

read from the

keyboard

reading data

from the file

Page 11: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 11/22

PROCESSING FILE

• Use file streams to read or write to a disk file

• Byte Streams : FileInputStream or

FileOutputStream

• Character Streams : FileReader or FileWriter

• The FileInputStream class represents an

InputStream that read data bytes from a file.

• The FileOutputStream class represents an

OutputStream that write data bytes to a file.

Page 12: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 12/22

PROCESSING FILE

• Constructor that can be used to create a file

stream :

 – FileInputStream(string filenameString) – FileOutputStream(string filenameString)

 – FileReader(string filenameString)

 –

FileWrite(string filenameString)

Page 13: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 13/22

ARRAY STREAMS

• It is used to read and write the data bytes or

characters from an array

• A program can change input from the entire

line at a time which from the input streams to

a byte array form

Page 14: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 14/22

ARRAY STREAMS

• Constructors that can be used to create arratstreams : – ByteArrayInputStream(byte[] byteArray);

 – ByteArrayOutputStream(byte[] byteArray);

 – ByteArrayReader(byte[] byteArray);

 – ByteArrayWriter(byte[] byteArray);

• E.g :

byte[] bArray = new byte[2048]ByteArrayInputStream b = newByteArrayInputStream(bArray);

Page 15: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 15/22

FILTER STREAMS

• FilterInputStream and FilterOutputStream

classes are main subclasses to InputStream

and OutputStream that modify the data from

a streams

• Can use a filter class to wrap an input stream

when read the integers, doubles or strings

data types.

Page 16: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 16/22

FILTER STREAMS

• FilterStream classes :

 – DataInputStream and DataOutputStream

 – BufferedInputStream and BufferedOutputStream

 – PrintStream

 – LineNumberInputStream

 – PushbackInputStream

 – CipherInputStream and CipherOutputStream

Page 17: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 17/22

DATA STREAMS

• Read and write Java primitive types in a

machine-independent fashion

• DataInputStream is used to read whereas the

DataOutput is used to write

• Method defined in the DataInput interface :

 – readByte(), readShort(), readInt(), readLong(),

readFloat(), readDouble(), readChar(),

readBoolead(), readUTF()

Page 18: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 18/22

DATA STREAMS

• Method defined in the DataOutput interface :

 – writeByte(), writeInt(), writeShort()

• Can use data I/O streams to create data

streams :

 – DataInputStream(InputStream Istream)

 – DataOutputStream(OutputStream outStream)

Page 19: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 19/22

PRINT STREAMS

• How to output the data into files and view it

as a text.

• Have TWO classes : PrintStream and

PrintWriter

• The java.io.PrintStream class is a subclass of 

FilterOutputStream

• It is implemented by System.out ans

System.err.

Page 20: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 20/22

PRINT STREAMS

• It allows a simple printing of both primitive values

which is objects and string literals.

• It use the platform’s default character encoding

yo convert characters value into bytes type

• This class traps all IOExceptions. However, the

error status can be tested with checkError()

method. This method will returns true value if anerror has occurred. Otherwise, the false value will

be returned.

Page 21: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 21/22

PRINT STREAMS

• Constructor that can be used (for PrintWriter)

 – PrintWriter(Write out)

 – PrintWriter(Write out, Boolean autoFlush)

 – PrintWriter(OutputStream out)

 – PrintWriter(OutputStream out, Boolean

autoFlash)

Page 22: Topic 2 Part2

7/30/2019 Topic 2 Part2

http://slidepdf.com/reader/full/topic-2-part2 22/22

SUMMARY

• In Java, streams are classified into two types

namely input stream and output stream.

•In Java source of input data is called Inputstream and source of output data is called

output stream.

 All input and output is done by using methodsthat are part of java.io package in Java.

• Input can be provided to Java programs

using command line arguments.