Download presentation
Presentation is loading. Please wait.
Published byΤρύφαινα Πρωτονοτάριος Modified over 5 years ago
1
OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11_1 GEORGE KOUTSOGIANNAKIS Copyright: Illinois Institute of Technology/ George Koutsogiannakis
2
Previous Lecture Topics
Using java.io stream classes. How to create file and write text. How to read a text file.
3
Topics of this Lecture Binary coded files.
Binary coding of primitive data types. Practiced example of writing text and reading text files (scanner will not be used from now on to read text files).
4
Binary Coded Files. A file stored in binary format. A binary file is computer -readable but not human-readable. All executable programs are stored in binary files, as are most numeric data files. In contrast, text files are stored in a form (usually ASCII) that is human-readable.
5
Binary Coded Files Every primitive data type can be converted into a binary coded version of the data type. The java.io package has classes with methods that allow us to convert each data type to its binary coded version and write it (or read it from) in a file. This technique can be used when structured data is to be recorded in a file.
6
Binary Coded Records Let us look at the example of structured data of records from lecture 11 again: An airline company could store data in a file where each line represents a flight segment containing the following data: flight number origin airport destination airport number of passengers average ticket price These are called headers or field names of the record. Such a file could contain the following data: AA123,BWI,SFO,235,239.5 AA200,BOS,JFK,150,89.3
7
Binary Coded Records The airline wants to save the records in a secured (coded) file. The records will have to be written into binary coded file using the proper streams. Each field of each record will need to be binary coded and written into the file. The file will be read back by a program which uses the proper read streams that convert the binary coded data back to its original data type.
8
Binary Coded data. To convert data type to their binary coded versions we use two classes out of the java.io pckage: A File object whose constructor takes as argument the name of the file to be created. The extension is usually .dat An object of the FileOutputStream class whose constructor takes as argument the File object created above in step 1. An object of the class DataOutputStream whose constructor takes as argument the FileOutputObject created in step 2.
9
Binary Coded Data The class DataOutputStream has a number of write methods. Actually one write method for each primitive data type. There are also other write methods that allow you to write an array of bytes types. You will invoke the proper write method and pass it the primitive data type that you want to convert and write into the file.
10
DataOutputStream Class Methods
void flush() Flushes this data output stream. int size() Returns the current value of the counter written, the number of bytes written to this data output stream so far. void write(byte[] b, int off, int len) Writes len bytes from the specified byte array starting at offset off to the underlying output stream. void write(int b) Writes the specified byte (the low eight bits of the argument b) to the underlying output stream. void writeBoolean(boolean v) Writes a boolean to the underlying output stream as a 1-byte value. void writeByte(int v) Writes out a byte to the underlying output stream as a 1-byte value. void writeBytes(String s) Writes out the string to the underlying output stream as a sequence of bytes. void writeChar(int v) Writes a char to the underlying output stream as a 2-byte value, high byte first. void writeChars(String s) Writes a string to the underlying output stream as a sequence of characters. void writeDouble(double v) Converts the double argument to a long using the doubleToLongBits method in class Double, and then writes that long value to the underlying output stream as an 8-byte quantity, high byte first. void writeFloat(float v) Converts the float argument to an int using the floatToIntBits method in class Float, and then writes that int value to the underlying output stream as a 4-byte quantity, high byte first. void writeInt(int v) Writes an int to the underlying output stream as four bytes, high byte first. void writeLong(long v) Writes a long to the underlying output stream as eight bytes, high byte first. void writeShort(int v) Writes a short to the underlying output stream as two bytes, high byte first. void writeUTF(String str) Writes a string to the underlying output stream using modified UTF-8 encoding in a machine-independent manner.
11
Reading Binary Coded Data
To read binary coded data from a binary coded file use assuming that the name of the file is numerical.dat: File file = new File ("numerical.dat"); FileInputStream file_input = new FileInputStream (file); DataInputStream data_in = new DataInputStream (file_input );
12
DataInputStream class
The DataInputStream class has read methods A read method for each primitive data type. When you open a file for reading you need to keep reading the coded values until the EOF (end of file) symbol is encoutered. When the EOF is encountered by your program an end of file exception will be thrown. The catch will be used to stop the reading
13
While loop try { //create stream objects while (true) { try {
//read method(s) //other code as needed } catch (EOFException eof) { System.out.println ("End of File"); break; // Print out the integer, double data pairs. System.out.printf ("%3d. Data = %8.3e %n", i_data, d_data ); } // end of while loop data_in.close (); } //end of top try catch (IOException e) { System.out.println ( "IO Exception =: " + e );
14
DataInputStream class
Some of the methods in this class are: int read(byte[] b) Reads some number of bytes from the contained input stream and stores them into the buffer array b. Int read(byte[] b, int off, int len) Reads up to len bytes of data from the contained input stream into an array of bytes. boolean readBoolean() See the general contract of the readBoolean method of DataInput. byte readByte() See the general contract of the readByte method of DataInput. char readChar() See the general contract of the readChar method of DataInput. double readDouble() See the general contract of the readDouble method of DataInput. float readFloat() See the general contract of the readFloat method of DataInput. void readFully(byte[] b) See the general contract of the readFully method of DataInput. void readFully(byte[] b, int off, int len) See the general contract of the readFully method of DataInput. int readInt() See the general contract of the readInt method of DataInput.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.