input/output input in java is stream based.a stream represents sequence of bytes or characters....

28
Input/output Input in java is stream based .A stream represents sequence of bytes or characters . Stream provides an abstract view of I/O. Stream can be of two types :1) Input Stream 2) Output Stream Input Stream are used to read data from a source ,Output Stream are to write data to a target. In java two types of stream are provided : 1) Byte oriented 2) Character Oriented(Add after jdk 1.2,support for Unicode character) .Character Stream supports Unicode character where as byte stream does not .

Upload: justin-douglas-casey

Post on 04-Jan-2016

227 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

Input/outputInput in java is stream based .A stream represents

sequence of bytes or characters . Stream provides an abstract view of I/O. Stream can be of two types :1)

Input Stream 2) Output StreamInput Stream are used to read data from a

source ,Output Stream are to write data to a target. In java two types of stream are provided : 1) Byte oriented 2) Character Oriented(Add after jdk

1.2,support for Unicode character) .Character Stream supports Unicode character where as byte stream

does not .

Page 2: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

Commonly used Byte oriented I/P Streams• 1)InputStream: is an abstract class that is

extended by all byte oriented input stream.• 2)OutputStream: is an abstract class that that is

extended by all byte oriented O/P stream.• 3) ByteArrayInputStream: An input Stream that is

used to read bytes from a byte array. • 4) BufferedInputStream: input Stream to read

bytes from a buffer.• 5) FileInputStream : input stream to read bytes

from a file.

Page 3: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• 6) ByteArrayOutPutStream :Output Stream to write bytes to a byte Array.

• 7)BufferedOutPutStream: output Stream to write bytes to a Buffer.

• 8)FileOutStream:OutPut Stream to write bytes to a file . Etc…..

• Input Stream class provides an abstract method read (). That is used to read bytes or bytes from an input stream . This method is define by all the subclass of input Stream.

• Syntax: public int read()throws IOException;

Page 4: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Reads a single bytes from the Stream & returns a integer. Returns -1 to denote end of stream.

• Public int read(byte [])throws IOException;// for block of bytes.

• Reads a block of bytes from the underlying stream & stores them in the specified array . Maximum bytes that can be read depends on the size of array as well as no of available bytes in the stream. Output stream class provides an abstract method write().that is used to write a bytes or block of bytes to stream .

Page 5: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Public void write (int byte) throws IOException;• Public void write(byte [])throws IOException;• General signature to create an object of an

InputStream : • Public type inputStream(Object Source );• Public type OutputStream(Object target);• Note: 1)Reading bytes from a byte array .• Byte a[]={1,2,3,4…….};• ByteArrayInputStream b=new

ByteArrayInputStream(a);// a is source.• b.read(); =>1• b.read(); =>2

Page 6: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• 2)Reading bytes from a file name a.txt• FileInputStream f=new FileInputStream(“a.txt”);• f.read();• 3) reading bytes from keyboard:-

BufferedInputStream b=new BufferedInputStream(System.in);//(System.in )is a keyboard buffer.

• b.read();

Page 7: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

Character based commonly used string classes

• 1) Reader is an abstract class that is extended by all character oriented InputStream .

• 2) Writer : abstract class that is extended by all character oriented output Stream.

• 3) CharacterArrayReader• 4) bBufferedReader • 5) FlieReader • 6)CharArrayWriter • 7)BufferedWritter

Page 8: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• 8) FileWriter • 9) InputStreamReader • 10) OutStreamReader• 11) PrintWriter .• InputstreamReader : is used to convert a byte

oriented input stream into a character oriented Stream( i.e. 8 ----16)

• OutputStreamWriter: it is used to convert character oriented OutputStream into a byte oriented output Stream (i.e 16 ------8)

Page 9: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Printwritter : is a character oriented version of PrintStream .

• Reader class provides abstract read() method that is used to read a character or block of characters from a input Stream.

• Public int read() throws IOException;• Public int read(char[])throws IOException;• Apart from read() method buffered reader class

provides a method name readLine() i.e. use to read a complete line of text from the stream.

• readLine():- public String readLine() throws IoException;

Page 10: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

Reading character or String from keyboard

• BufferedReader b=new BufferedReader (System.in); //can’t compile due to incomputable.

• InputStreamReader in=new InputStreamReader(System.in);

• bufferedReader b=new BufferedReader (in);• //or • BufferedReader b=new bufferedReader(new

InputStreamreader(System.in));•

Page 11: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

System. in

BufferedReader(for Stream)

IputStreamReader(it makes Character Stream)

Page 12: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Note: All these I/O Stream classes are defind in java.io package.

• For better performance we chose another appraoch.

BufferedReader

InputStreamReader(For changing character oriented)

FileInputStream(its provide read())

File

Page 13: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

Or• Above and this pictures shows the solutions of

line by line reading.

BufferedReader

FileReader

File

Page 14: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• InputStream class provides a method name available() that is used to find out the number of available bytes in a input stream.

• Public int available() throws IOException;• Note: we have two classes PrintWritter &

PrintStream for showing output on screen.

Printwritter

FileOutPutStream

File

Page 15: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

SequenceInputStreamIt is provides the facility of sequentially reading

data from multiple input stream .A sequential input stream is used to treat multiple input stream as a

single input stream.Sequence input Stream

Input Stream

Input Stream

Input Stream

Source

Source

source

Page 16: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Public SequenceInputStream(InputStream Stream1, InputStream Stream2);

• Public SequenceInputStream(enumeration Streams);

• Enumeration means list name • There are approaches for reading a file 1) one

by one 2) sequence

Page 17: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

PipedInputStream & PipedOutPutStream• These stream classes are used to connect an input

stream to a output Stream i.e. data being return to an output stream can be concurrently read by an input stream.

InputStream OutputStream

Pipe

Page 18: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Public PipedInputStream();• Public PipedInputStream(pipedoutputStream

out);• Public PipedoutStream();• Public PipedoutStream(pipedinputStream in);

Page 19: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

File

Reads data from InputStream

convert it into upper case and

stored it into a file

Reads data from file writes it into piped

outputStream

File

File Input stream

PipedOutPutStream

Thread 1Thread 2

Page 20: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Object InputStream & ObjectOutputStream : class provides the facility of deserialiazing and serialing objects respectively.

• Serialization is a process of converting an object into a stream in such a manner that object can be reconstructed from the stream .Deserialization is the process of constructing an object from a serialized stream.

• ObjectOutputStream class provides writeObject() method that is used to serialize an object .

• Public void writeObject(Object o)throws NotSerializableException ;

Page 21: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Note: By default Object of a class is not serializable i.e. it can not be converted into a Stream by writeObject() of ObjectOutputStream class.

• Inorder to serialize objects of a class java.io serializable interface has to be Implemented in the class.

• It is a marker Interface ;• Public interface serializable marker interface is

an interface that does not have any members. The marker is used to mark a class as a member of a group.

Page 22: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• readObject() method of object input stream class is used deserialize an object.

• Public object readObject();• transient Keyword:it is used to mark those data

members of a serializable object that are not to be saved while object is serilized.

• Note: composite object can only be serialized if all its constituent members are seriliazable.

• Ex: class A• {• Int a,b;• }

Page 23: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Class B implements Serializable• {• A a;• Int c;• Public B()• {• a=new A();• }• }• B x=new B();

x

a 20

cb

a

100 200

Composite object

Page 24: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• X can not be serialized because its member a is not serializable .

• java.util.scanner :- is added in jdk1.5 to facilitate reading data from a Stream as primitive type or string . This class provides following methods:

• 1) public int nextInt();• Public byte nextByte();• Public float nextFloat();• Public double nextDouble();• Public string nextLine();• Etc….

Page 25: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• Note 1:if we want to read data line by line line then we use BufferedReader InputStream .

• We can wrap any object in its.• Note2: if we want to write data line by line then

we use printStream or PrintWritterOutPutStream. We can wrap any object in its.

Page 26: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

Java.io.File :- class • Object of file class represents a file or directory

in a file system this class provides methods that are used to obtain information about a file or directory as well as methods to create new folders, to change the name & folders & file or diroctory , to remove etc….

• Cons: public File(String path);• Public File(File path, String name);

Page 27: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

Method of File class :- • Public boolean isFile();• Public boolean isDirectory();• Public boolean exists();• Public boolean isReadOnly();• Public bollean isSystem();• Public boolean isHidden();• Public String getName();• Public String getPath();• Public string[] list();

Page 28: Input/output Input in java is stream based.A stream represents sequence of bytes or characters. Stream provides an abstract view of I/O. Stream can be

• If file object is a folder then this method returns an any of String Object . Each element of the array represent the file or folder that is stored under the folder represented by file object.

• Public boolean mkdir();• Public boolean reNmaeTo();• Public boolean renameTo(File new name );• Public boolean delete();