17
JAVA I/O © EnhanceEdu, IIIT Hyderabad

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

Embed Size (px)

Citation preview

Page 1: 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

JAVA I/O

© EnhanceEdu, IIIT Hyderabad

Page 2: 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

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

Page 3: 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

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.

Page 4: 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

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.

Page 5: 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

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

Page 6: 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

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)

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

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.

Page 8: 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

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)

Page 9: 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

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.

Page 10: 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

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.

Page 11: 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

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

Page 12: 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

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.

Page 13: 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

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.

Page 14: 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

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

Page 15: 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

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

Page 16: 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

Exceptions in I/O

3/29/2010EnhanceEdu, IIIT - H

16

Page 17: 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

THANK YOU