Streams and InputOutput Files Part 3 1 Handling

  • Slides: 22
Download presentation
Streams and Input/Output Files Part 3 1

Streams and Input/Output Files Part 3 1

Handling Primitive Data Types n n n The basic input and output streams provide

Handling Primitive Data Types n n n The basic input and output streams provide read/write methods that can be used for reading/writing bytes or characters. To read/write the primitive data types such as integers and doubles, we can use filter classes as wrappers on the existing I/O streams to filter data to the original stream. The two filter classes supported for creating “data streams” for primitive data types are: n n Data. Input. Stream Data. Output. Stream 2

Hierarchy of Data Stream Classes Class Interface Filter. Input. Stream Data. Input. Stream Class

Hierarchy of Data Stream Classes Class Interface Filter. Input. Stream Data. Input. Stream Class Interface Filter. Output. Stream Data. Outputt. Stream 3

Data Input Stream Creation n Create Input File Stream: n n Create Input Data

Data Input Stream Creation n Create Input File Stream: n n Create Input Data Stream: n n n Data. Input. Stream dis = new Data. Input. Stream( fis ); The above statements wrap data input stream (dis) on file input stream (fis) and use it as a “filter”. Methods Supported: n n File. Input. Stream fis = new File. Input. Stream(“In. File”); read. Boolean(), read. Byte(), read. Char(), read. Short(), read. Int(), read. Long(), read. Float(), read. Double() They read data stored in file in binary format. 4

Data Output Stream Creation n Create Output File Stream: n n Create Output Data

Data Output Stream Creation n Create Output File Stream: n n Create Output Data Stream: n n n Data. Output. Stream dos = new Data. Output. Stream( fos ); The above statements wrap data output stream (dos) on file output stream (fos) and use it as a “filter”. Methods Supported: n n File. Output. Stream fos = new File. Output. Stream(“Out. File”); write. Boolean(), write. Byte(), write. Char(), write. Short(), write. Int(), write. Long(), write. Float(), write. Double() They write data to file in binary format. n How many bytes are written to file when for statements: n write. Int(120), write. Int(10120) 5

Data Streams Flow via Filter n Write primitive data to the file using a

Data Streams Flow via Filter n Write primitive data to the file using a filter. Program n dos filter binary stream mydata Read primitive data from the file using a filter. mydata fis dis binary stream filter Program Screen 6

Writing and Reading Primitive Data import java. io. *; public class Read. Write. Filter

Writing and Reading Primitive Data import java. io. *; public class Read. Write. Filter { public static void main(String args[]) throws IOException { // write primitive data in binary format to the "mydata" file File. Output. Stream fos = new File. Output. Stream("mydata"); Data. Output. Stream dos = new Data. Output. Stream(fos); dos. write. Int(120); dos. write. Double(375. 50); dos. write. Int('A'+1); dos. write. Boolean(true); dos. write. Char('X'); dos. close(); fos. close(); } } // read primitive data in binary format from the "mydata" file File. Input. Stream fis = new File. Input. Stream("mydata"); Data. Input. Stream dis = new Data. Input. Stream(fis); System. out. println(dis. read. Int()); System. out. println(dis. read. Double()); System. out. println(dis. read. Int()); System. out. println(dis. read. Boolean()); System. out. println(dis. read. Char()); dis. close(); fis. close(); 7

Program Run and Output n C: 254examples>java Read. Write. Filter n n n 120

Program Run and Output n C: 254examples>java Read. Write. Filter n n n 120 375. 5 66 true X Display content of “mydata” file (in binary format): n C: 254examples>type mydata n n x@wx B☺ X What is the size of “mydata” file (in bytes) ? n Size of int+double+int+boolean+char 8

Concatenating and Buffering Streams n n Two or more input streams can be combined

Concatenating and Buffering Streams n n Two or more input streams can be combined into a single input stream. This process is known as logical concatenation of streams and is achieved using the Sequence. Input. Stream class. A Sequence. Input. Stream starts out with an ordered collection of input streams and reads from the first one until end of file is reached, whereupon it reads from the second one, and so on, until end of file is reached on the last of the contained input streams. 9

Sequencing and Buffering of Streams n Buffered streams sit between the program and data

Sequencing and Buffering of Streams n Buffered streams sit between the program and data source/destination and functions like a filter or support efficient I/O. Buffered can be created using Buffered. Input. Stream and Buffered. Output. Stream classes. file 1. dat file 2. dat file 1+file 2 Buffer Streams Sequencer in. Buffer read() Program write() Buffer Screen output. Buffer 10

Example Program import java. io. *; public class Combine. Streams { public static void

Example Program import java. io. *; public class Combine. Streams { public static void main(String args[]) throws IOException { // declare file streams File. Input. Stream file 1 = new File. Input. Stream("file 1. dat"); File. Input. Stream file 2 = new File. Input. Stream("file 2. dat"); // declare file 3 to store combined streams Sequence. Input. Stream file 3 = null; // concatenate file 1 and file 2 streams into file 3 = new Sequence. Input. Stream(file 1, file 2); Buffered. Input. Stream in. Buffer = new Buffered. Input. Stream(file 3); Buffered. Output. Stream out. Buffer = new Buffered. Output. Stream(System. out); // read and write combined streams until the end of buffers int ch; while((ch = in. Buffer. read()) != -1 ) out. Buffer. write(ch); out. Buffer. flush(); // check out the output by removing this line System. out. println("n. Hello, This output is generated by Combine. Files. java program"); in. Buffer. close(); out. Buffer. close(); file 1. close(); file 2. close(); file 3. close(); } } 11

Contents of Input Files n The file 1. dat contains: n n n Hello,

Contents of Input Files n The file 1. dat contains: n n n Hello, I am C++, born in AT&T. The file 2. dat contains: n n Hello, I am Java, born in Sun Microsystems! 12

Output n C: 254examples>java Combine. Streams n n n Hello, I am C++, born

Output n C: 254examples>java Combine. Streams n n n Hello, I am C++, born in AT&T. Hello, I am Java, born in Sun Microsystems! Hello, This output is generated by Combine. Files. java program If the statement out. Buffer. flush() is removed, the output will be: n n n Hello, This output is generated by Combine. Files. java program Hello, I am C++, born in AT&T. Hello, I am Java, born in Sun Microsystems! 13

Random Access Files n n So for we have discussed sequential files that are

Random Access Files n n So for we have discussed sequential files that are either used for storing data and accessed (read/write) them in sequence. In most real world applications, it is necessary to access data in non-sequential order (e. g, banking system) and append new data or update existing data. Java IO package supports Random. Access. File class that allow us to create files that can be used for reading and/or writing with random access. The file can be open either in read mode (“r”) or readwrite mode (“rw”) as follows: n n my. File. Handle. Name = new Random. Access. File (“filename”, “mode”); The file pointer can be set to any location (measured in bytes) using seek() method prior to reading or writing. 14

Random Access Example import java. io. *; public class Random. Access { public static

Random Access Example import java. io. *; public class Random. Access { public static void main(String args[]) throws IOException { // write primitive data in binary format to the "mydata" file Random. Access. File myfile = new Random. Access. File("rand. dat", "rw"); myfile. write. Int(120); myfile. write. Double(375. 50); myfile. write. Int('A'+1); myfile. write. Boolean(true); myfile. write. Char('X'); // set pointer to the beginning of file and read next two items myfile. seek(0); System. out. println(myfile. read. Int()); System. out. println(myfile. read. Double()); //set pointer to the 4 th item and read it myfile. seek(16); System. out. println(myfile. read. Boolean()); // Go to the end and “append” an integer 2003 myfile. seek(myfile. length()); myfile. write. Int(2003); // read 5 th and 6 th items myfile. seek(17); System. out. println(myfile. read. Char()); System. out. println(myfile. read. Int()); System. out. println("File length: "+myfile. length()); myfile. close(); } } 0 Int 4 Double Int boolean Char Int 12 16 17 19 23 15

Execution and Output n C: 254examples>java Random. Access n n n 120 375. 5

Execution and Output n C: 254examples>java Random. Access n n n 120 375. 5 true X 2003 File length: 23 16

Streams and Interactive I/O n n Real world applications are designed to support interactive

Streams and Interactive I/O n n Real world applications are designed to support interactive and/or batch I/O operations. Interactive programs allow users to interact with them during their execution through I/O devices such as keyboard, mouse, display devices (text/graphical interface), media devices (microphones/speakers), etc. . n n Java provides rich functionality for developing interactive programs. Batch programs are those that are designed to read input data from files and produce outputs through files. 17

Standard I/O n The System class contains three I/O objects (static) n n System.

Standard I/O n The System class contains three I/O objects (static) n n System. in – instance of Input. Stream System. out – instance of Print. Stream System. err – instance of Print. Stream To perform keyboard input, we need use functionalities of Data. Input. Stream and String. Tokenizer classes. 18

Reading Integer from Standard Input n Create buffered reader for standard input by wrapping

Reading Integer from Standard Input n Create buffered reader for standard input by wrapping System. in object: n n Read a line of text from the console n n String str = dis. read. Line(); Create Tokenens n n n Buffered. Reader dis = new Buffered. Reader(new Input. Stream. Reader(System. in)); String. Tokenizer st; st = new String. Tokenizer(str); Convert String Token into basic integer: n int std. ID = Integer. parse. Int(st. next. Token()); 19

Interactive IO Example import java. io. *; import java. util. *; public class Student.

Interactive IO Example import java. io. *; import java. util. *; public class Student. Record { public static void main(String args[]) throws IOException { // Create buffered reader for standard input Buffered. Reader dis = new Buffered. Reader(new Input. Stream. Reader(System. in)); String. Tokenizer st; // reading data from console System. out. print("Enter Student ID: "); st = new String. Tokenizer(dis. read. Line()); int std. ID = Integer. parse. Int(st. next. Token()); System. out. print("Enter Student Name: "); String std. Name = dis. read. Line(); System. out. print("Enter Student Marks: "); st = new String. Tokenizer(dis. read. Line()); int std. Marks = Integer. parse. Int(st. next. Token()); // write to console System. out. println("Student details are: "); System. out. println("ID: "+std. ID); System. out. println("Name: "+std. Name); System. out. println("Marks: "+std. Marks); } } 20

Run and Output n C: 254examples>java Student. Record n n n n Enter Student

Run and Output n C: 254examples>java Student. Record n n n n Enter Student ID: 2002010 Enter Student Name: Mary Baker Enter Student Marks: 85 Student details are: ID: 2002010 Name: Mary Baker Marks: 85 21

Summary n n n n All Java I/O classes are designed to operate with

Summary n n n n All Java I/O classes are designed to operate with Exceptions. User Exceptions and your own handler with files to manger runtime errors. Subclasses File. Reader / File. Writer support charactersbased File I/O. File. Input. Stream and File. Output. Stream classes support bytes-based File I/O. Buffered read/write operations support efficient I/O. Data. Input. Stream and Data. Output. Stream classes support rich I/O functionality. Random. Access. File supports access to any data items in files in any order. 22