Presentation is loading. Please wait.

Presentation is loading. Please wait.

BINARY I/O IN JAVA CSC 202 November What should be familiar concepts after this set of topics: All files are binary files. The nature of text files.

Similar presentations


Presentation on theme: "BINARY I/O IN JAVA CSC 202 November What should be familiar concepts after this set of topics: All files are binary files. The nature of text files."— Presentation transcript:

1 BINARY I/O IN JAVA CSC 202 November 2013

2 What should be familiar concepts after this set of topics: All files are binary files. The nature of text files. Difference between text I/O and binary I/O. Java classes for I/O. How to write a program that reads and writes binary (non- text) data. How to encrypt data. Buffers CSC 102 Computer Science Principles 2

3 Text I/O & Binary I/O Binary data in character representation (text) Easily readable by a human. Binary data not in character representation Not easily readable by a human. Some classes are used to handle text data. Other classes are used for binary data. 3

4 Text I/O & Binary I/O Everything is stored in binary. Includes data in text files. However, text file contents are stored in binary patterns that represent characters. So, we can use Scanner to read characters from text files and PrintWriter to write characters to text files. See TextFileInOutDemo.java 4

5 Binary I/O More efficient than text I/O Text I/O: Representation of 199 in character format (e.g., ASCII) requires 3 bytes (one for each character). Binary I/O: Only one byte required to represent 199 as a binary value in hex. Independent of encoding scheme Doesn’t matter whether ASCII or some other scheme (e.g., EBCDIC) is used. 5

6 Binary I/O More efficient than text I/O Text I/O: Representation of 199 in character format (e.g., ASCII) requires 3 bytes (one for each character). Binary I/O: Only one byte required to represent 199 as a binary value in hex. Why is this more efficient? Independent of encoding scheme Doesn’t matter whether ASCII or some other scheme (e.g., EBCDIC) is used. 6

7 Binary I/O More efficient than text I/O Text I/O: Representation of 199 in character format (e.g., ASCII) requires 3 bytes (one for each character). Binary I/O: Only one byte required to represent 199 as a binary value in hex. Why is this more efficient? Less storage required. Fewer bytes to transfer between memory and disk; thus, faster data transfer. Independent of encoding scheme Doesn’t matter whether ASCII or some other scheme (e.g., EBCDIC) is used. 7

8 Binary I/O Classes See class hierarchy, Fig. 19.4, p. 652, 8 th ed.; Fig. 19.3, p. 712, 9 th ed. FileInputStream/FileOutputStream FilterInputStream ^ DataInputStream Note that DataInputStream and DataOutputStream implement the DataInput and DataOutput interfaces, respectively. See API. 8

9 Writing Characters and Strings (using binary I/O) writeChar (char c) – writes 16-bit Unicode character writeChars (String s) – writes the characters in the String as 16-bit Unicode characters writeUTF (String s) – writes UTF (16-bit length followed by UTF-8 format of each character in the string) First 2 bytes tell number of characters in the string UTF-8 character requires 1, 2, or 3 bytes First bits of UTF-8 character tell the number of bytes: 0 => 1; 110 => 2; 1110 => 3 Text Example: writeUTF(“ABCDEF”); /* writes 00 06 41 42 43 44 45 46 (all in hexadecimal) 00 06 means 6 characters; 41 is ASCII for an ‘A’ (the first bit is a zero, which means the character takes 1 byte), and so on. */ 9

10 Buffers are better! FilterInputStream ^ BufferedInputStream Buffers speed up I/O of large files tremendously. Default buffer size: 512 bytes Buffer size can be specified. See API. 10

11 Exceptions Most methods in the I/O classes throw IOException. Requires throws clause or try-catch block End of File can be detected by encountering an EOFException. See Listing 19.3. 11

12 Other Note that import java.io.*; is required. It’s important to use the.close() method after finished writing to an output file. Why? 12


Download ppt "BINARY I/O IN JAVA CSC 202 November What should be familiar concepts after this set of topics: All files are binary files. The nature of text files."

Similar presentations


Ads by Google