ch23c-binary

Download Report

Transcript ch23c-binary

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
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.
Binary Files (continued)
• A data input stream lets an application
read primitive Java data types from an
underlying input stream in a machineindependent 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.
The DataInputStream/DataOutputStream
classes, provide methods for doing I/O
of primitive types in _______ format.
(a) ASCII
(b) Character
(c) binary
(d) Unicode
Binary Files (continued)
classes DataInputStream and DataOutputStream
java.io
Constructors
DataInputStream(InputStream in)
Creates a DataInputStream that uses the specified underlying
InputStream.
DataOutputStream(OutputStream out)
Creates a new data output stream to write data to the specified
underlying output stream.
Binary Files (continued)
classes DataInputStream and DataOutputStream
java.io
Methods
void close() throws IOException
Closes a stream and releases any system resources associated with the
stream.
int read(byte[] b) throws IOException
Reads some number of bytes from the input stream and stores them
into the byte array b. The return value is the number of bytes actually
read or -1 if there is no more data because the end of the stream has
been reached.
void
write(byte[] b, int off, int len) throws IOException
Writes len bytes from the specified byte array starting at array index
off to the underlying output stream.
Binary Files (continued)
classes DataInputStream and DataOutputStream
java.io
Methods (continued)
int readInt() throws IOException
Reads four input bytes and returns an int value in a machineindependent fashion. This method is suitable for reading bytes written
by the writeInt() method. Throws EOFException if this file reaches
the end before reading four bytes.
void
writeInt(int v) throws IOException
Writes an int to the underlying output stream as four bytes.
long readLong() throws IOException
Reads eight input bytes and returns a long value in a machineindependent fashion. This method is suitable for reading bytes written
by the writeLong() method. Throws EOFException if this file reaches
the end before reading eight bytes.
void
writeLong(long v) throws IOException
Writes a long to the underlying output stream as eight bytes.
Binary Files (continued)
classes DataInputStream and DataOutputStream
java.io
Methods (continued)
short readShort() throws IOException
Reads two input bytes and returns a short value in a machineindependent fashion. This method is suitable for reading bytes written
by the writeShort() method. Throws EOFException if this file reaches
the end before reading two bytes.
void
writeShort(int v) throws IOException
Writes a short to the underlying output stream as two bytes.
int available() throws IOException
Returns the number of bytes that can be read from this input stream
without blocking.
Circle all classes, which are declared
by Java as Abstract Classes
OutputStream
DataInputStream
InputStream
FileOutputStream
ObjectInputStream
Program 23.1 (Run)
int:
100
short: 1500
long:
4294967295
byte array: 3 5 2
7
15
100
127
55
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 = 4294967295L;
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;
Program 23.1 (continued)
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);
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());
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();
}
}
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"));
File Compression
• Lossless compression loses no data and
is used for data backup.
File Compression (continued)
• Lossy compression is used for
applications like sound and video
compression and causes minor loss of
data.
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
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)
0
75
0
40
0
(b)
75
40
101
3
80
(c)
75
0
40
0
0
(d)
75
0
0
0
40
101
0
0
101
0
0
0
3
0
3
101
80
0
3
80
0
0
0
80