input/output system java programming - gujarat · session 3 – b: i/o system java programming tcs...

37
Session 3 – B: I/O System Java Programming 1 TCS Confidential Input/Output System Java Programming

Upload: hadang

Post on 05-Jul-2019

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming1TCS Confidential

Input/Output System

Java Programming

Page 2: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming2TCS Confidential

Input/Output in JavaThe Java I/O SystemStream TypesThe Byte Stream HierarchyPredefined Streams in JavaThe File ClassByte Stream Hierarchy Classes

Page 3: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming3TCS Confidential

The Java I/O System

JavaProgram

Hard Disk

Memory

Page 4: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming4TCS Confidential

The Java I/O SystemStreamsJava I/OPhysical Devices

Input StreamOutput Stream

JavaProgram

File

MemoryStream (Java I/O API)

Page 5: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming5TCS Confidential

Stream TypesTwo types of streams• Byte based

Used to read/write byte data (0-255)

• Character based Used to read/write textual,

Unicode data

Page 6: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming6TCS Confidential

Byte StreamsMain abstract classes

InputStreamOutputStream

Concrete stream classes for handling various devices (e.g. disk files, memory buffers)

Page 7: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming7TCS Confidential

Character StreamsMain abstract classes

ReaderWriter

Concrete subclasses implements required methods

Page 8: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming8TCS Confidential

Abstract ClassesStream Types – Java I/O

Streams

Byte Based

Character Based

InputStream

OutputStream

Reader

Writer

Page 9: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming9TCS Confidential

The Byte Stream Hierarchy

BufferedOutputStreamPrintStream

Object

InputStream OutputStreamFileInputStreamFilterInputStreamByteArrayInputStreamSequenceInputStreamPipedInputStream

FileOutputStreamFilterOutputStream

ByteArrayOutputStreamPipedOutputStream

BufferedInputStreamPushbackInputStream

Page 10: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming10TCS Confidential

Java I/O MethodsCommonly Used Methods

InputStreamread(), close()

OutputStreamwrite(), flush(), close()

Page 11: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming11TCS Confidential

Selecting Stream ClassFor best performance, use the most specific stream class possible

For file read/write use FileInputStream/ FileOutputStream

Page 12: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming12TCS Confidential

Predefined Streams in Javajava.lang.System class

System contains 3 predefined public and static stream variables, in, out, and err.

System.in: is of type InputStreamSystem.out, System.err:

are of type PrintStream

Page 13: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming13TCS Confidential

Predefined Streams in Java …Used to read-write characters from-to the consoleread() - the basic method

public abstract int read()throws IOException

Page 14: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming14TCS Confidential

Reading Console InputSystem.in andBufferedReaderBufferedReader’s int read( )method reads a characterread() method performs

‘blocking read’ - waits for data if there is none available

Page 15: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming15TCS Confidential

Reading Console Input…A single byte is read from the input stream, converted into an integer (0 to 255)-1 is returned on the end of streamreadLine() method reads a line

Page 16: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming16TCS Confidential

Demo: Reading ConsoleIt reads and displays characters typed by userWhen ‘q’ is typed, the program exits

Page 17: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming17TCS Confidential

Reading Console Input…System.in is line bufferedNo input is actually passed to the program until ENTER is pressed.This makes readLine()valuable than read() for interactive console input

Page 18: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming18TCS Confidential

Demo: Read LineProgram reads a line from the console and displays that lineWhen ‘stop’ is entered then program exits

Page 19: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming19TCS Confidential

Writing Console OutputUse print() and println()methods of class PrintStream.write() method of PrintStreamclass – a character-based alternative to print on console

void write(int byteval) throws IOException

Page 20: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming20TCS Confidential

Writing Console Output…public class WriteDemo {public static void main

(String args[]){int b; b = ‘A’;System.out.write(b);System.out.write(‘\n’);

}}

Page 21: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming21TCS Confidential

Writing Console Output ...

print() and println() of System.out are recommended for debugging purposes or for sample programs

Page 22: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming22TCS Confidential

Reading & Writing FilesFileInputStream and FileOutputStream classesCreate byte streams linked to files

FileInputStream(String fileName)

throws FileNotFoundException

FileOutputStream(String fileName)

throws FileNotFoundException

Page 23: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming23TCS Confidential

Reading & Writing Files…read()/write() methodsRemember… File should be closed after its use Use close() method

void close() throws IOException

Page 24: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming24TCS Confidential

Demo: Count BytesThe program counts the total number of bytes coming from a file, or console if no file is specified as a command line argument

Page 25: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming25TCS Confidential

Demo: Copy FileThe program copies a file to another fileTwo command line arguments are required

Page 26: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming26TCS Confidential

The File ClassIs a subclass of Object that encapsulates a disk file

It neither operates on streams nor specifies how information is retrieved from or stored in files

Page 27: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming27TCS Confidential

The File Class …

A file object is used to obtain/ manipulate file permissions, time, date, and directory pathnavigate subdirectory hierarchies

Page 28: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming28TCS Confidential

The File Class …Constructors to create Fileobjects:

File(String dirPath)File(String dirPath, String fname)File(File dirObj, String fname)

Page 29: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming29TCS Confidential

Demo: File PropertiesProgram displays properties of a file supplied as first command line argumentIt copies to another file supplied as second command line argumentDeletes a file supplied as third command line argument

Page 30: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming30TCS Confidential

Demo: FileIOProgram expects two command line argumentsCopies first file contents to second file

Page 31: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming31TCS Confidential

Filtered I/O StreamsFilterInputStream and FilterOutputStream are streams that modify the information sent through an existing stream

Page 32: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming32TCS Confidential

Filtered I/O Streams ...BufferedInputStream and BufferedOutputStream allow attaching a memory buffer to the I/O streams

Page 33: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming33TCS Confidential

Filtered I/O Streams ...The PrintStream class provides all of the formatting capabilities we have been using from System.out

Page 34: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming34TCS Confidential

ByteArray I/O StreamsByteArrayInputStream,ByteArrayOutputStream are subclasses of InputStreamand OutputStream that support I/O from/to byte array

ByteArrayInputStream(byte array [])

Page 35: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming35TCS Confidential

SequenceInputStreamBuilds one stream from multiple input streams

Useful when reading from multiple data files is required

SequenceInputStream(InputStreamis1, InputStream is2)

Page 36: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming36TCS Confidential

Demo: SequenceInputStreamProgram expects two command line arguments

Reads both file contents and prints on the console

Page 37: Input/Output System Java Programming - Gujarat · Session 3 – B: I/O System Java Programming TCS Confidential 2 Input/Output in Java The Java I/O System Stream Types The Byte Stream

Session 3 – B: I/O System Java Programming37TCS Confidential

SummaryThe Java I/O SystemStream TypesThe Byte Stream HierarchyPredefined Streams in JavaThe File ClassByte Stream Hierarchy Classes