150 likes | 388 Views
Binary I/O in Java. CSC 202 November 2013. 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.
E N D
Binary I/O in Java CSC 202 November 2013
CSC 102 Computer Science Principles 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
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.
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
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.
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.
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.
Binary I/O Classes • See class hierarchy, Fig. 19.4, p. 652, 8th ed.; Fig. 19.3, p. 712, 9th ed. • FileInputStream/FileOutputStream <bytes> • FilterInputStream ^DataInputStream<primitive types, strings> Note that DataInputStream and DataOutputStream implement the DataInput and DataOutput interfaces, respectively. See API.
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. */
Buffers are better! • FilterInputStream ^BufferedInputStream <primitive types, strings> • Buffers speed up I/O of large files tremendously. • Default buffer size: 512 bytes • Buffer size can be specified. See API.
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.
Other • Note that import java.io.*; is required. • It’s important to use the .close() method after finished writing to an output file. • Why?