Presentation is loading. Please wait.

Presentation is loading. Please wait.

Podcast Ch23c Title: Binary Files

Similar presentations


Presentation on theme: "Podcast Ch23c Title: Binary Files"— Presentation transcript:

1 Podcast Ch23c Title: Binary Files
Description: Overview of binary files; DataInputStream; DataOutputStream; Program 23.1; file compression Participants: Barry Kurtz (instructor); John Helfert and Tobie Williams (students) Textbook: Data Structures for Java; William H. Ford and William R. Topp

2 Binary Files File types are text files and binary files. Java deals with files by creating a byte stream that connects the file and the application. Binary files can be handled with DataInputStream and DataOutputStream classes.

3 Binary Files (continued)
A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. A data output stream lets an application write primitive Java data types to an output stream in a portable way. An application can then use a data input stream to read the data back in.

4 The DataInputStream/DataOutputStream classes, provide methods for doing I/O of primitive types in _______ format. ASCII (b) Character binary (d) Unicode

5 Binary Files (continued)

6 Binary Files (continued)

7 Binary Files (continued)

8 Binary Files (continued)

9 Circle all classes, which are declared by Java as Abstract Classes
OutputStream DataInputStream InputStream FileOutputStream ObjectInputStream

10 Program 23.1 (Run) int: 100 short: 1500 long: 4294967295
byte array:

11 Program 23.1 import java.io.*; public class Program23_1 {
public static void main(String[] args) throws IOException int intVal = 100; short shortVal = 1500; long longVal = L; byte[] buf = {3, 5, 2, 7, 15, 100, 127, 55}; // create a DataOutputStream that writes to // the file "data.dat" in the local directory DataOutputStream fout = null; // use to input data from "data.dat" DataInputStream fin = null;

12 Program 23.1 (continued) { fout = new DataOutputStream(
try { fout = new DataOutputStream( new FileOutputStream("data.dat")); } catch (FileNotFoundException fnfe) System.err.println("Cannot create \"data.dat\""); System.exit(1); // write each variable and the array to f fout.writeInt(intVal); fout.writeShort(shortVal); fout.writeLong(longVal); fout.write(buf, 0, buf.length);

13 Program 23.1 (continued) // close the stream and open it
// as a DataInputStream fout.close(); try { fin = new DataInputStream(new FileInputStream( "data.dat")); } catch (FileNotFoundException fnfe) { System.err.println("Failure to open " + "\"data.dat\""); System.exit(1); // input the int, short, and long from the file System.out.println("int: " + fin.readInt()); System.out.println("short: " + fin.readShort()); System.out.println("long: " + fin.readLong());

14 Program 23.1 (concluded) // input the byte array that was written
// to the file; the number of bytes in the // array is the number of bytes remaining // unread in the file byte[] b = new byte[fin.available()]; System.out.print("byte array: "); // input the array fin.read(b); // output the bytes for (int i=0; i < b.length; i++) System.out.print(b[i] + " "); System.out.println(); // close the stream fin.close(); }

15 Which of the following statements correctly creates a DataOutputStream that writes data to the file named "out.dat"? (a) DataOutputStream dOut = new DataOutputStream ( new InputStream("out.dat")); (b) DataOutputStream dOut = new DataOutputStream( new FileOutputStream("out.dat")); (c) DataOutputStream dOut = new DataOutputStream ("out.dat"); (d) DataOutputStream dOut = (DataOutputStream) new FileOutputStream("out.dat"));

16 File Compression Lossless compression loses no data and is used for data backup.

17 File Compression (continued)
Lossy compression is used for applications like sound and video compression and causes minor loss of data.

18 File Compression (continued)
The compression ratio is the ratio of the number of bits in the original data to the number of bits in the compressed image. For instance, if a data file contains 500,000 bytes and the compressed data contains 100,000 bytes, the compression ratio is 5:1

19 Assume dOut is a DataOutputStream
Assume dOut is a DataOutputStream. The "write" statements copy data for primitive byte, short, and int variables to a file. byte bVal = 75; short shVal = 40; int intVal = 101; dOut.writeByte(bVal); dOut.writeShort(shVal); dOut.writeInt(intVal); dOut.writeByte(bVal/20); dOut.writeShort(shVal*2); What is the contents of file as displayed in individual bytes with decimal values (a) (b)


Download ppt "Podcast Ch23c Title: Binary Files"

Similar presentations


Ads by Google