290 likes | 529 Views
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
E N D
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 .
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.
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;
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 .
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
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();
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
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)
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;
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));
BufferedReader(for Stream) IputStreamReader(it makes Character Stream) System. in
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
Or • Above and this pictures shows the solutions of line by line reading. BufferedReader FileReader File
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
SequenceInputStream Sequence input Stream Source Input Stream It 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. Source Input Stream source Input Stream
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
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
Public PipedInputStream(); • Public PipedInputStream(pipedoutputStream out); • Public PipedoutStream(); • Public PipedoutStream(pipedinputStream in);
File File File Input stream Thread 2 Thread 1 Reads data from InputStream convert it into upper case and stored it into a file Reads data from file writes it into piped outputStream PipedOutPutStream
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 ;
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.
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 • { • Inta,b; • }
20 a a • Class B implements Serializable • { • A a; • Int c; • Public B() • { • a=new A(); • } • } • B x=new B(); x b c 100 200 Composite object
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 intnextInt(); • Public byte nextByte(); • Public float nextFloat(); • Public double nextDouble(); • Public string nextLine(); • Etc….
Note 1:if we want to read data line by line line then we use BufferedReaderInputStream . • 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.
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);
Method of File class :- • Public booleanisFile(); • Public booleanisDirectory(); • Public boolean exists(); • Public booleanisReadOnly(); • Public bolleanisSystem(); • Public booleanisHidden(); • Public String getName(); • Public String getPath(); • Public string[] list();
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 booleanmkdir(); • Public booleanreNmaeTo(); • Public booleanrenameTo(File new name ); • Public boolean delete();