using input output

28
Using I/O

Upload: raksharao

Post on 13-Aug-2015

31 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: Using Input Output

Using I/O

Page 2: Using Input Output

• Java program performs I/O through streams.

• A stream is an abstraction that either produces or consumes information.

• A stream is linked to physical device by java I/O system.

• Java implements streams within class hierarchies defined in the java.io package.

Page 3: Using Input Output

Byte streams and character streams

• Java defines two types of streams i.e. byte streams and character stream

• Byte streams handles input and output of bytes. Eg: used when reading or writing binary data.

• Character stream handles input and output of character.

• Character streams are efficient than byte streams.

Page 4: Using Input Output

Byte stream classes

• Defined by two class hierarchies

• Top level classes : InputStream and OutputStream

• InputStream defines the charecterstics common to byte input streams.

• OutputStream defines the charecterstics common to byte output streams.

Page 5: Using Input Output

Java I/O – InputStreams

Page 6: Using Input Output

Java I/O – OutputStreams

Page 7: Using Input Output

Byte stream classes

• BufferedInputStream-• BufferedOutputStream- • ByteArrayInputStream- input stream that

reads from a byte array• ByteArrayOutputStream-Output stream

that writes a byte array• DataInputStream- an input stream that

contains methods for reading the java standard data type.

Page 8: Using Input Output

• DataOutputStream- an Output stream that contains methods for writing the java standard data type.

• FileInputStream-Input stream that reads a file

• FileOutputStream-Output stream that writes a file

Page 9: Using Input Output

• FilterInputStream- implements InputStream• FilterOutputStream- implements OutputStream• InputStream- abstract class that describes

stream input• ObjectInputStream- input stream for objects• ObjectOutputStream- output stream for objects• OutputStream - abstract class that describes

stream output

Page 10: Using Input Output

• PipedInputStream- Input pipe• PipedOutputStream- Output pipe• PrintStream –Output stream that contains print()

and println()• PushbackInputStream – Input stream that allows

bytes to be returned to the stream.• SequenceInputStream- input stream that is a

combination of two or more input streams that will read sequentially one after the other.

Page 11: Using Input Output

Character stream classes

• Defined using two class heirachies topped by two abstract classes :

• Reader and writer

• Reader is used for input

• Writer is used for output

Page 12: Using Input Output

Java I/O – Readers

Page 13: Using Input Output

Java I/O – Writers

Page 14: Using Input Output

• BufferedReader- buffered input character stream

• BufferedWriter- Buffered output character stream

• CharArrayReader- Input Stream that reads character array

• CharArrayWriter- Output Stream that writes character array

Page 15: Using Input Output

• FileReader- Input stream that reads a file

• FileWriter- Output stream that writes a file

• FilterReader- filtered reader

• FilterWriter- filtered writer

• InputStreamReader- Input stream that translates bytes to charecters

• LineNumberReader- Input stream that counts line number.

Page 16: Using Input Output

• OutputSreamWriter- Output stream that translates character to bytes

• PipedReader- input pipe• PipedWriter- output pipe• PrintWriter- Output stream that contains println()

and print()• PushbackReader- Input stream that allows

charecters to be returned to input stream• Reader- Abstract class that decribes character

stream input.

Page 17: Using Input Output

• StringReader- Input stream that reads from a string

• StringWriter- Output stream that writes to a string

• Writer- Abstract class that describes character stream output.

Page 18: Using Input Output

The Predefined Streams

• Java automatically imports a package called java.lang package

• It defines a class called System.• It contains three predefined stream

variables called in , out and err• These fields are defined as public, final and

static within System • Means that they can be used by any other

part of the program

Page 19: Using Input Output

• System.out refers to standard output stream by default it is console.

• System.in refers to standard input by default keyboard

• System.err refers to error stream, by default it is console.

• System.in is an object of InputStream• System.out and System.err are objects of

type PrintStream

Page 20: Using Input Output

Methods defined by InputStream

Method Description

int avaiable() Returns the number of bytes of input currently available for reading

void close() Close the input source

void mark(int numbytes) Places a mark at the current point in the input stream that will remain valid until numBytes bytes are read.

boolean marksupported()

Returns true if mark() / reset() are supported by the invoking stream.

int read() Returns an integer representation of the next available byte of input.-1 is returned when end of the stream is encountered.

int read(byte buffer[]) Attempts to read up to buffer.length bytes into buffer and returns the actual number of bytes that were successfully read

Page 21: Using Input Output

int read(byte buffer[],int offset,int numbytes)

Attempts to read the numBytes bytes into buffer starting at buffer[offset], returning the number of bytes successfully read.

void reset() Resets the input pointer to the previously set mark

long skip(long numbytes)

Ignores numBytes bytes of input, returning the number of bytes actually ignored.

Page 22: Using Input Output

Methods of OutputStream

Method Desccription

void close() Closes the output stream

void flush() Causes an output that has been buffered to be sent to its destination

void write(int b ) Writes a single byte to an output stream

void write(byte buffer[])

Writes a complete array of bytes to an output stream

void write(byte buffer[],int offset,int numBytes)

Writes a subrange of numBytes from array buffer beginning at buffer[offset]

Page 23: Using Input Output

Reading Console Input

• Byte or character oriented streams

• Preferred method to read console input is to use a character oriented stream

• Reason– makes program easier to internationalize – easier to maintain.

Page 24: Using Input Output

• System.in is an instance of InputStream• InputStream defines only one method called read()

which reads byte• Three versions of read() exists• int read() throws IOException• int read(byte data[])throws IOException• int read(byte data[], int start,int max) throws IOException• Reading from System.in,pressing Enter generates end of

stream condition

Page 25: Using Input Output

• int read() throws IOException– Reads a single character from keyboard, returns -1 if it reaches

end of stream

• int read(byte data[])throws IOException– Reads bytes from input stream and puts them into data until

either array is full , the end of stream is encountered or error occurs.

• int read(byte data[], int start,int max) throws IOException– Reads input into data beginning at the location specified by start,

upto max bytes are stored. – It returns number of byte read or -1 when end of stream is

reached.

Page 26: Using Input Output

import java.io.*;class ReadBytes{public static void main(String args[]) throws IOException{

byte data[]=new byte[10];

System.out.println("Enter the charecters:");System.in.read(data);

System.out.println("You entered:");for(int i=0;i<=data.length;i++)

System.out.println((char)data[i]);}//end of main}//end of class

Page 27: Using Input Output

Writing console output

• print() and println() are used for writing to console• Defined in PrintStream class• PrintStream is an output stream derived from

OutputStream it also implements the low-level method called write()

• void write(int byteVal);• This method writes the byte specified by byteval to the

file • There are two more functions, printf and format which

allows us to write a formatted output into the console.

Page 28: Using Input Output

class WriteDemo{public static void main(String args[]){

int b;b='X';System.out.write(b);System.out.write('\n');

}//end of main}//end of class