Chapter 17 Binary I - O PPT Download
Chapter 17 Binary I - O PPT Download
Search... Search
Download presentation
Buttons:
Cancel Download
1 / 36
Similar presentations
Chapter 17 Binary I/O 1.
Embed
Download presentation
6
2 Motivations
Data stored in a textDownload
file is represented in human-readable form. Data
presentation
stored in a binary file is represented in binary form. You cannot read
binary files. They are designed to be read by programs. For example, Java
source programs are We stored
thinkin you
texthave
files liked
and can
thisbe read by a text
presentation. editor,
If you wish to
but Java classes are stored in binary files and are read by the JVM. The
download it, please recommend it to your friends in any social
advantage of binary files is that
system. they
Share are more
buttons are aefficient
little bitto process
lower. Thankthan
you!
text files.
2 Buttons:
5 Binary I/O
Text I/O requires encoding and decoding. The JVM converts a Unicode to
a file specific encoding when writing a character and converts a file
specific encoding to a Unicode when reading a character. Binary I/O does
not require conversions. When you write a byte to a file, the original byte
is copied into the file. When you read a byte from a file, the exact byte in
the file is returned.
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.
5
9 FileInputStream/FileOutputStream
FileInputStream/FileOutputStream associates a binary input/output 6
stream with an external file. All the methods in
FileInputStream/FileOuptputStream are inherited from its superclasses.
9
Cancel Download
10 FileInputStream
To construct a FileInputStream, use the following constructors:
public FileInputStream(String filename)
public FileInputStream(File file)
A java.io.FileNotFoundException would occur if you attempt to create a
FileInputStream with a nonexistent file.
10
11 FileOutputStream
To construct a FileOutputStream, use the following constructors:
public FileOutputStream(String filename)
public FileOutputStream(File file)
public FileOutputStream(String filename, boolean append)
public FileOutputStream(File file, boolean append)
If the file does not exist, a new file would be created. If the file already
exists, the first two constructors would delete the current contents in the
file. To retain the current content and append new data into the file, use
the last two constructors by passing true to the append parameter.
11
12 FilterInputStream/FilterOutputStream
Filter streams are streams that filter bytes for some purpose. The basic
byte input stream provides a read method that can only be used for
reading bytes. If you want to read integers, doubles, or strings, you need
a filter class to wrap the byte input stream. Using a filter class enables
you to read integers, doubles, and strings instead of bytes and
characters. FilterInputStream and FilterOutputStream are the base
classes for filtering data. When you need to process primitive numeric
Totypes, usewebsite
make this DatInputStream and
work, we log user DataOutputStream
data to filter
and share it with processors. bytes.
To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.
12
13 DataInputStream/DataOutputStream
DataInputStream reads bytes from the stream and converts them into
Download presentation
appropriate primitive type values or strings.
DataOutputStream converts primitive type values or strings into bytes
and output the bytes to
Wethe stream.
think you have liked this presentation. If you wish to
13 download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!
14 DataInputStream
Buttons:FilterInputStream and implements the
DataInputStream extends
DataInput interface.
14
6
15 DataOutputStream
DataOutputStream extends FilterOutputStream and implements the
DataOutput interface.
15 Cancel Download
17 Using DataInputStream/DataOutputStream
Data streams are used as wrappers on existing input and output streams
to filter data in the original stream. They are created using the following
constructors:
public DataInputStream(InputStream instream)
public DataOutputStream(OutputStream outstream)
The statements given below create data streams. The first statement
creates an input stream for file in.dat; the second statement creates an
output stream for file out.dat.
DataInputStream infile =
new DataInputStream(new FileInputStream("in.dat"));
DataOutputStream outfile =
new DataOutputStream(new FileOutputStream("out.dat"));
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.
17
19 BufferedInputStream/ BufferedOutputStream
Using buffers to speed up I/O 6
BufferedInputStream/BufferedOutputStream does not contain new
methods. All the methods BufferedInputStream/BufferedOutputStream
are inherited from the InputStream/OutputStream classes. Cancel Download
19
20 Constructing BufferedInputStream/BufferedOutputStream
// Create a BufferedInputStream
public BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in, int bufferSize)
// Create a BufferedOutputStream
public BufferedOutputStream(OutputStream out)
public BufferedOutputStream(OutputStream out, int bufferSize)
20
21 Optional
Object I/O
DataInputStream/DataOutputStream enables you to perform I/O for
primitive type values and strings.
ObjectInputStream/ObjectOutputStream enables you to perform I/O for
objects in addition for primitive type values and strings.
21
22 ObjectInputStream
ObjectInputStream extends InputStream and implements ObjectInput
and ObjectStreamConstants.
22
23 ObjectOutputStream
ObjectOutputStream extends OutputStream and implements
ObjectOutput and ObjectStreamConstants.
23
24
To make thisUsing
websiteObject Streams
work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.
You may wrap an ObjectInputStream/ObjectOutputStream on any
InputStream/OutputStream using the following constructors:
// Create an ObjectInputStream
public ObjectInputStream(InputStream in)
Download
// Create an ObjectOutputStream presentation
public ObjectOutputStream(OutputStream out)
24
We think you have liked this presentation. If you wish to
download it, please recommend it to your friends in any social
25 The Serializable Interface
system. Share buttons are a little bit lower. Thank you!
Not all objects can be written to an output stream. Objects that can be
written to an object stream is said to be serializable. A serializable object
Buttons:
is an instance of the java.io.Serializable interface. So the class of a
serializable object must implement Serializable.
The Serializable interface is a marker interface. It has no methods, so you
don't need to add additional code in your class that implements 6
Serializable.
Implementing this interface enables the Java serialization mechanism to
automate the process of storing the objects and arrays.
Cancel Download
25
28 Serializing Arrays
An array is serializable if all its elements are serializable. So an entire
array can be saved using writeObject into a file and later restored using
readObject. Here is an example that stores an array of five int values and
an array of three strings, and reads them back to display on the console.
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.
28
31 File Pointer
6
A random access file consists of a sequence of bytes. There is a special
marker called file pointer that is positioned at one of these bytes. A read
or write operation takes place at the location of the file pointer. When a
file is opened, the file pointer sets at the beginning of the file. When
Cancelyou Download
read or write data to the file, the file pointer moves forward to the next
data. For example, if you read an int value using readInt(), the JVM reads
four bytes from the file pointer and now the file pointer is four bytes
ahead of the previous location.
31
32 RandomAccessFile Methods
Many methods in RandomAccessFile are the same as those in
DataInputStream and DataOutputStream. For example, readInt(),
readLong(), writeDouble(), readLine(), writeInt(), and writeLong() can be
used in data input stream or data output stream as well as in
RandomAccessFile streams.
32
35 RandomAccessFile Constructor
RandomAccessFile rafDownload
= new RandomAccessFile("test.dat", "rw"); // allows
presentation
read and write
RandomAccessFile raf = new RandomAccessFile("test.dat", "r"); // read
only We think you have liked this presentation. If you wish to
35 download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!
36 Checkpoint
Buttons:
Why do you have to throw i/o exception when using i/o classes ?
How do you check EOF ?
What are the differences between text i/o and binary i/o?
6
Cancel Download
Download ppt "Chapter 17 Binary I/O 1."
Similar presentations
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.
Download presentation
Buttons:
Cancel Download
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.
Download presentation
Buttons:
Cancel Download
Search... Search
To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie
policy. I agree.