Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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.

2 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.

3 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;

4 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.

5 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

6 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();

7 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 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)

9 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;

10 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));

11 System. in BufferedReader(for Stream) IputStreamReader(it makes Character Stream)

12 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

13 Or Above and this pictures shows the solutions of line by line reading. BufferedReader FileReader File

14 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

15 SequenceInputStream 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. Sequence input Stream Input Stream Source source

16 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

17 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. InputStreamOutputStream Pipe

18 Public PipedInputStream(); Public PipedInputStream(pipedoutputStream out); Public PipedoutStream(); Public PipedoutStream(pipedinputStream in);

19 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 PipedOutPut Stream Thread 1 Thread 2

20 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 ;

21 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.

22 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; }

23 Class B implements Serializable { A a; Int c; Public B() { a=new A(); } B x=new B(); x a 20 c b a 100 200 Composite object

24 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….

25 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.

26 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);

27 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();

28 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();


Download ppt "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."

Similar presentations


Ads by Google