0% found this document useful (0 votes)
20 views10 pages

Chapter 17 Binary I - O PPT Download

Uploaded by

Subrahmanya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views10 pages

Chapter 17 Binary I - O PPT Download

Uploaded by

Subrahmanya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Log in 

Search... Search
Download presentation

We think you have liked this presentation. If you wish to


download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!

Buttons:

Cancel Download

1 / 36

Similar presentations
Chapter 17 Binary I/O 1. 

Published by Lesley Robertson Modified over 4 years ago

 Embed

 Download presentation
6

Presentation on theme: "Chapter 17 Binary I/O 1."—


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
Presentation transcript: policy. I agree.
1 Chapter 17 Binary I/O 1

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:

3 How is I/O Handled in Java?


A File object encapsulates the properties of a file or a path, but does not
6
contain the methods for reading/writing data from/to a file. In order to
perform I/O, you need to create objects using appropriate Java I/O
classes.
Scanner input = new Scanner(new File("temp.txt")); Cancel Download
System.out.println(input.nextLine());
PrintWriter output = new PrintWriter("temp.txt");
output.println("Java 101");
output.close();
3

4 Text File vs. Binary File


Data stored in a text file are represented in human-readable form. Data
stored in a binary file are represented in binary form. You cannot read
binary files. Binary files are designed to be read by programs. For
example, the Java source programs are stored in text files and can be
read by a text editor, but the Java classes are stored in binary files and
are read by the JVM. The advantage of binary files is that they are more
efficient to process than text files.
Although it is not technically precise and correct, you can imagine that a
text file consists of a sequence of characters and a binary file consists of a
sequence of bits. For example, the decimal integer 199 is stored as the
sequence of three characters: '1', '9', '9' in a text file and the same integer
is stored as a byte-type value C7 in a binary file, because decimal 199
equals to hex C7.
4

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

6 Binary I/O Classes 6



Download presentation
7 InputStream
The value returned is a byte as an int type.
7 We think you have liked this presentation. If you wish to
download it, please recommend it to your friends in any social
8 OutputStream system. Share buttons are a little bit lower. Thank you!
The value is a byte as an int type.
8 Buttons:

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

16 Characters and Strings in Binary I/O


A Unicode consists of two bytes. The writeChar(char c) method writes the
Unicode of character c to the output. The writeChars(String s) method
writes the Unicode for each character in the string s to the output.
Why UTF-8? What is UTF-8?
UTF-8 is a coding scheme that allows systems to operate with both ASCII
and Unicode efficiently. Most operating systems use ASCII. Java uses
Unicode. The ASCII character set is a subset of the Unicode character set.
Since most applications need only the ASCII character set, it is a waste to
represent an 8-bit ASCII character as a 16-bit Unicode character. The UTF-
8 is an alternative scheme that stores a character using 1, 2, or 3 bytes.
ASCII values (less than 0x7F) are coded in one byte. Unicode values less
than 0x7FF are coded in two bytes. Other Unicode values are coded in
three bytes.
16

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

18 Order and Format Checking End of File


CAUTION: You have toDownload
read the data in the same order and same format 
presentation
in which they are stored. For example, since names are written in UTF-8
using writeUTF, you must read names using readUTF.
Checking End of File We think you have liked this presentation. If you wish to
TIP: If you keep reading data atit,the
download endrecommend
please of a stream,it to
anyour
EOFException
friends in any social
would occur. So howsystem.
do you check the end of a file? You can use
Share buttons are a little bit lower. Thank you!
input.available() to check it. input.available() == 0 indicates that it is the
end of a file. Buttons:
18

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

26 The transient Keyword


If an object is an instance of Serializable, but it contains non-serializable
instance data fields, can the object be serialized? The answer is no. To
enable the object to be serialized, you can use the transient keyword to
mark these data fields to tell the JVM to ignore these fields when writing
the object to an object stream.
26

27 The transient Keyword, cont.


Consider the following class:
public class Foo implements java.io.Serializable {
private int v1;
private static double v2;
private transient A v3 = new A();
}
class A { } // A is not serializable
When an object of the Foo class is serialized, only variable v1 is serialized.
Variable v2 is not serialized because it is a static variable, and variable v3
is not serialized because it is marked transient. If v3 were not marked
transient, a java.io.NotSerializableException would occur.
27

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

29 Random Access Files


All of the streams youDownload
have used presentation
so far are known as read-only or write- 
only streams. The external files of these streams are sequential files that
cannot be updated without creating a new file. It is often necessary to
modify files or to insert new
We think yourecords
have likedinto
thisfiles. Java provides
presentation. the to
If you wish
RandomAccessFile class to allow
download it, aplease
file torecommend
be read from it toand
yourwrite to in
friends at any social
random locations. system. Share buttons are a little bit lower. Thank you!
29
Buttons:
30 RandomAccessFile 30

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

33 RandomAccessFile Methods, cont.


void seek(long pos) throws IOException;
Sets the offset from the beginning of the RandomAccessFile stream to
where the next read or write occurs.
long getFilePointer() IOException;
Returns the current offset, in bytes, from the beginning of the file to
where the next read or write occurs.
33

34 RandomAccessFile Methods, cont.


long length()IOException
Returns the length of the file.
final void writeChar(int v) throws IOException
Writes a character to the file as a two-byte Unicode, with the high byte
written first.
final void writeChars(String s) throws IOException
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
Writes a string to the file as a sequence of characters. policy. I agree.
34

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

We think you have liked this presentation. If you wish to


download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!

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

We think you have liked this presentation. If you wish to


download it, please recommend it to your friends in any social
system. Share buttons are a little bit lower. Thank you!

Buttons:

Cancel Download

© 2022 SlidePlayer.com Inc. Feedback Do Not Sell About project


All rights reserved.
Privacy Policy My Personal SlidePlayer
Feedback Information Terms of Service

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.

You might also like