Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.

Similar presentations


Presentation on theme: "JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and."— Presentation transcript:

1 JAVA I/O © EnhanceEdu, IIIT Hyderabad

2 Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and High level]  Character Streams [Low level and High level]  Serialization  Exceptions in I/O

3 Command Line I/O 3/29/2010EnhanceEdu, IIIT - H 3  Command Line I/O deals with reading and writing data n console  A console is a control unit, such as a terminal, through which a user communicates with a computer. [Command prompt in Windows NT/2000/XP and Terminal window or shell in UNIX]  System class is used to read/write on console. “System.in” is the standard input object and “System.out” is used for standard output.  System.in returns InputStream.  System.in.read() and System.out.print() and their variants are used to read and write on console respectively.

4 File class 3/29/2010EnhanceEdu, IIIT - H 4  File class is an abstract representation of file and directory pathnames.  The class doesn't provide direct access for reading and writing file data  Java provides a system independent way of dealing with pathnames  createNewFile() method of File class is used to create new files on disk and mkdir() is used to create a new directory.  delete() method delete the particular file/directory.  To delete a directory, the files and folders present in it needs to be deleted first.

5 Streams 3/29/2010EnhanceEdu, IIIT - H 5  All fundamental I/O in Java is based on Streams.  A stream is a channel of communication between source and destination.  For each read/write operation three steps are performed:  Open stream  Read/write data  Close stream  Streams are classified into Byte streams(low and high level) and Character streams (low and high level).

6 Byte Streams 3/29/2010EnhanceEdu, IIIT - H 6  Byte streams are used to read/write data in bytes.  Byte streams contain two class hierarchies with two abstract classes on top: Inputstream and Outputstream.  Byte streams are classified into Low –level Byte Stream and High-level Byte Streams.  Low-level Byte Streams reads and writes raw bytes whereas High-level Byte Streams read/write bytes in a better format.  High-level streams are usually used in combination with other stream (especially low-level streams)

7 Low-level Byte Streams FileInputStream/FileOutputStream 3/29/2010EnhanceEdu, IIIT - H 7  They are byte-oriented streams and extend the Input/output functionalities of InputStream and OutputStream to read/write content from files  Usually chained with high-level streams (especially with DataInputStream and DataOutputStream) to perform formatted I/O on files.  Often constructed by supplying filename (as string) or an object of File class.  The read() method of FileInputStream is used to read a byte of data from InputStream and read(byte[] b) is used to read data in array of bytes.  The write(int b) method of FileOutputStream is used to write single byte and write(byte[] b) is used to write array of bytes to OutputStream.

8 High-level Byte Streams DataInputStream/DataOutputStream 3/29/2010EnhanceEdu, IIIT - H 8  DataInputStream and DataOutputStream are subclasses of filter streams (FilterInputStream and FilterOutputStream)  They provide methods to read/write strings and primitive (high-level) data types such as int, float, double, etc.  Used in combination with low-level byte streams.  Constructed by passing objects of InputStream/OutputStream  Commonly used methods are writeInt(int i), writeChar(int c), writeDouble(double d), and writeUTF(String str)

9 Character Streams 3/29/2010EnhanceEdu, IIIT - H 9  Character streams work with Unicode characters and read/write text.  Character streams contain two class hierarchies with two abstract classes on top: Reader and Writer.  Character streams are classified into low-level Readers/Writers and high-level Readers/Writers.  Low-level character Streams reads/writes characters. High-level character streams read/write characters in a better format.

10 Low-level Character Streams FileReader/FileWriter 3/29/2010EnhanceEdu, IIIT - H 10  FileReader and FileWriter are subclasses of InputStreamReader and OutputStreamWriter respectively.  InputStreamReader/OutputStreamWriter, which are inherited from Reader/Writer, act as bridge between byte stream and character stream.  They read bytes and decode them into characters using a specified charset.  FileReader/FileWriter are meant for reading/writing streams of characters on files.

11 Low-level Character Streams FileReader/FileWriter (continued)  Usually constructed by passing filename (as String) or File object.  The read() method of FileReader is used to read a character from file.  To write a character on file write() method of FileWriter is used.  Data can also be appended to the file using append() method of FileWriter if file is opened in append mode.  read(char[] buf) method is used to read array of characters.  write(char[] buf) is used to write an array of characters. 3/29/2010EnhanceEdu, IIIT - H 11

12 High-level Character Streams BufferedReader/BufferedWriter 3/29/2010EnhanceEdu, IIIT - H 12  BufferedReader and BufferedWriter are used to read/write data in chunks or blocks (buffers) of characters.  They are subclasses of Reader and Writer respectively.  Size of the buffer can be specified while creating an object of BufferedReader/ BufferedWriter.  Usually chained with FileReader and FileWriter for performing file I/O. Also, chained with InputStreamReader to read from console.

13 High-level Character Streams (continued) BufferedReader/BufferedWriter 3/29/2010EnhanceEdu, IIIT - H 13  Takes an object of Reader/Writer (respectively) as parameter in constructors to instantiate.  The method readLine() of BufferedReader is used to read a line of text.  A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.  The method newLine() of BufferedWriter is used to write a line separator.  To write a string, write(String str) method is used.

14 Serialization  The process of saving the state of object(s) onto files at disk is known as Serialization.  The object to be serialized should implement the Serializable interface.  If a class is serializable, its subclasses automatically become serializable.  Transient and static variables are not saved during serialization.  Serialization also captures and serializes the object references contained in the serialized object. 3/29/2010EnhanceEdu, IIIT - H 14

15 Serialization ObjectInputStream/ObjectOutputStream  ObjectInputStream and ObjectOutputStream are specialized classes used to perform Serialization.  They take InputStream/OutputStream objects as parameters to instantiate.  Usually used in combination with FileInputStream and FileOutputStream.  The method writeObject(Object obj) of ObjectOutputStream is used to write (serialize) objects.  The method readObject() of ObjectInputStream is used to read (de-serialize) objects. 3/29/2010EnhanceEdu, IIIT - H 15

16 Exceptions in I/O 3/29/2010EnhanceEdu, IIIT - H 16

17 THANK YOU


Download ppt "JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and."

Similar presentations


Ads by Google