Chapter 6 Files and Streams
Chapter 6 Files and Streams
Files and
1 Streams
Objectives
Become familiar with the concept of an I/O
stream
Understand the difference between binary
files and text files
Learn how to save data in a file
Learn how to read data from a file
Outline
Overview of Streams and File I/O
Text-File I/O
Using the File Class
Basic Binary-File I/O
Object I/O with Object Streams
I/O Overview
I/O = Input/Output
In this context it is input to and output from programs
Input can be from keyboard or a file
Output can be to display (screen) or a file
Advantages of file I/O
permanent copy
output from one program can be input to another
input can be automated (rather than entered
manually)
Streams
Stream: an object that either delivers data to its
destination (screen, file, etc.) or that takes data from a
source (keyboard, file, etc.)
it acts as a buffer between the data source and
destination
Input stream: a stream that provides input to a
program
System.in is an input stream
Output stream: a stream that accepts output from a
program
System.out is an output stream
A stream connects a program to an I/O object
System.out connects a program to the screen
System.in connects a program to the keyboard
Binary Versus Text Files
All data and programs are ultimately just zeros and ones
each digit can have one of two values, hence binary
bit is one binary digit
byte is a group of eight bits
Text files: the bits represent printable characters
one byte per character for ASCII, the most common code
for example, Java source files are text files
so is any file created with a "text editor"
Binary files: the bits represent other types of encoded
information, such as executable instructions or numeric data
these files are easily read by the computer but not humans
they are not "printable" files
actually, you can print them, but they will be
unintelligible
"printable" means "easily readable by humans when
printed"
Text File I/O
Important classes for text file output (to the file)
PrintWriter
FileOutputStream [or FileWriter]
Important classes for text file input (from the file):
BufferedReader
FileReader
FileOutputStream and FileReader take file names as
arguments.
PrintWriter and BufferedReader provide useful methods
for easier writing and reading.
Usually need a combination of two classes
To use these classes your program needs a line like the
following:
import java.io.*;
Buffering
Not buffered: each byte is read/written from/to disk as
soon as possible
“little” delay for each byte
A disk operation per byte---higher overhead
Buffered: reading/writing in “chunks”
Some delay for some bytes
Assume 16-byte buffers
Reading: access the first 4 bytes, need to wait for all
16 bytes are read from disk to memory
Writing: save the first 4 bytes, need to wait for all 16
bytes before writing from memory to disk
A disk operation per a buffer of bytes---lower overhead
Every File Has Two Names
1.the stream name used by Java
outputStream in the example
PrintWriter FileOutputStream
Memory Disk
smileyOutStream smiley.txt
print
format
flush: write buffered output to disk
close: close the PrintWriter stream (and file)
TextFileOutputDemo
Part 1
A try-block is a block:
public static void main(String[] args) outputStream would
{ not be accessible to the
PrintWriter outputStream = null; rest of the method if it
try were declared inside the
{ Opening the file try-block
outputStream =
new PrintWriter(new
FileOutputStream("out.txt")); Creating a file can cause the
} FileNotFound-Exception if
catch(FileNotFoundException e) the new file cannot be made.
{
System.out.println("Error opening the file out.txt. “
+ e.getMessage());
System.exit(0);
}
TextFileOutputDemo
Part 2
System.out.println("Enter three lines of text:");
String line = null;
int count;
for (count = 1; count <= 3; count++)
{
line = keyboard.nextLine(); Writing to the file
outputStream.println(count + " " + line);
}
outputStream.close(); Closing the file
System.out.println("... written to out.txt.");
}
The println method is used with two different
streams: outputStream and System.out
Gotcha: Overwriting a File
Opening an output file creates an empty file
Two reasons:
1. To make sure it is closed if a program ends abnormally
(it could get damaged if it is left open).
BufferedReader FileReader
Memory Disk
smileyInStream smiley.txt