1 / 28

Input/output

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

maura
Télécharger la présentation

Input/output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. BufferedReader(for Stream) IputStreamReader(it makes Character Stream) System. in

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

  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. InputStream OutputStream Pipe

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

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

  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 • { • Inta,b; • }

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

  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 intnextInt(); • 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 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.

  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 booleanisFile(); • Public booleanisDirectory(); • Public boolean exists(); • Public booleanisReadOnly(); • Public bolleanisSystem(); • Public booleanisHidden(); • 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 booleanmkdir(); • Public booleanreNmaeTo(); • Public booleanrenameTo(File new name ); • Public boolean delete();

More Related