Chapter 5 Streams and File IO
Chapter 5 Streams and File IO
Chapter 5 Streams and File IO
Discuss on:
• What are the core reasons we use packages in
programming?
Chapter 5 • What elements typically reside within a package?
Streams and File I/O • What are some of the most common built-in
packages you've encountered in programming?
What specific tasks or functionalities do they
offer?
• What are the primary motivations for using
abstraction in programming?
• What are the key differences and use cases for
abstract classes and interfaces?
1 2
3 4
1
10/3/2024
• The ASCII or Unicode code for character 1 is 49 (0x31 in hex) and for character
9 is 57 (0x39 in hex). Thus, to write the characters 199, three bytes— 0x31,
0x39, and 0x39—are sent to the output, as shown in Figure (a) below.
• 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.
• a byte-type value 199 is represented as 0xC7 in the memory and appears Binary I/O is more efficient than text I/O, because binary I/O does not require
encoding and decoding. Binary files are independent of the encoding scheme on
exactly as 0xC7 in the file, as shown in Figure (b) below. the host machine and thus are portable. Java programs on any machine can read a
binary file created by a Java program. This is why Java class files are binary files.
Java class files can run on a JVM on any machine.
7 8
2
10/3/2024
a file.
• The Scanner class uses for reading text data from a file and
the PrintWriter class uses for writing text data to a file.
11 12
3
10/3/2024
Example: wring data using PrintWriter Example: reading data using Scanner
13 14
4
10/3/2024
InputStream
OutputStream
The value returned is a byte as an int type.
19 20
5
10/3/2024
FileInputStream/FileOutputStream 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 non-existent file.
If the file does not exist, a new file would be created. // Create an input stream for the file
FileInputStream input = new FileInputStream("temp.dat");
If the file already exists, the first two constructors would delete the // Read values from the file
int value;
while ((value = input.read() )!= -1)
current contents in the file. System.out.print(value + " "); This program uses binary
I/O to write ten byte
To retain the current content and append new data into the file, use the // Close the output stream values from 1 to 10 to a
input.close();
file named temp.dat and
last two constructors by passing true to the append parameter. }
} reads them back from
23 the file. 24
6
10/3/2024
FilterInputStream/FilterOutputStream 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 types, use
DataInputStream and DataOutputStream to filter bytes.
25 26
DataInputStream/DataOutputStream DataInputStream
DataInputStream reads bytes from the stream and
converts them into appropriate primitive type values
or strings. • DataInputStream extends FilterInputStream and implements
the DataInput interface.
7
10/3/2024
29 30
8
10/3/2024
Example: DataInputStream/DataOutputStream
import java.io.*; Order and Format
public class TestDataStream {
public static void main(String[] args) throws IOException {
// Create an output stream for file temp.dat
DataOutputStream output = new DataOutputStream(new
CAUTION: You have to read the data in the same order and
FileOutputStream("temp.dat")); same format in which they are stored. For example, since
// Write student test scores to the file
output.writeUTF("John"); names are written in UTF-8 using writeUTF, you must read
output.writeDouble(85.5); This program writes student names using readUTF.
output.writeUTF("Susan"); names and scores to a file
output.writeDouble(185.5); named temp.dat and reads the
output.writeUTF("Kim");
output.writeDouble(105.25);
// Close output stream
data back from the file.
Checking End of File
output.close();
// Create an input stream for file temp.dat
DataInputStream input = new DataInputStream(new TIP: If you keep reading data at the end of a stream, an
FileInputStream("temp.dat"));
EOFException would occur. So how do you check the end
// Read student test scores from the file
System.out.println(input.readUTF()+ " " + input.readDouble());
of a file? You can use input.available() to check it.
System.out.println(input.readUTF() + " " + input.readDouble()); input.available() == 0 indicates that it is the end of a file.
System.out.println(input.readUTF() + " " + input.readDouble());
}
} 33 34
BufferedInputStream/BufferedOutputStream BufferedInputStream/BufferedOutputStream
• BufferedInputStream/BufferedOutputStream can be used to speed up
input and output by reducing the number of disk reads and writes.
• The individual data are then delivered to your program from the buffer, as
shown in Figure (a) below.
9
10/3/2024
Constructing
BufferedInputStream/BufferedOutputStream
BufferedInputStream/BufferedOutputStream
// Create a BufferedInputStream
public BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in, int bufferSize)
// Create a BufferedOutputStream
public BufferedOutputStream(OutputStream out)
public BufferedOutputStream(OutputStreamr out, int bufferSize)
37 38
10
10/3/2024
43 44
11