CMP202 Lecture 5 - Text File IO
CMP202 Lecture 5 - Text File IO
II
LECTURE SERIES
OVERVIEW OF THE COURSE OUTLINE
Principles of Good programming
Structured programming concepts
Errors and Debugging
Testing
Text Files and IO
Text File I/O
• The java.io package contains nearly every class needed to perform input and
output (I/O) in Java. All these streams represent an input source and an output
destination.
• The stream in the java.io package supports many data such as primitives,
object, localized characters, etc.
• Stream
• A stream can be defined as a sequence of data. There are two kinds of
Streams:
• InPutStream: The InputStream is used to read data from a source. This stream is used
for reading data from the files.
• Objects can be created using the keyword new and there are several types of constructors available.
• OutPutStream: The OutputStream is used for writing data to a destination. This stream
is used to create a file and write data into it.
• The stream would create a file, if it doesn't already exist, before opening it for output.
Stream
• Java provides strong but flexible support for I/O related to files.
• The Files can be classified into two major categories: Binary files and Text
files.
• A binary file is a file whose contents must be handled as a sequence of binary digits.
• A text file is a file whose contents are to be handled as a sequence of characters.
• Why use files for I/O?
• Files provide permanent storage of data.
What are Standard Streams?
• All the programming languages provide support for standard I/O where
the user's program can take input from a keyboard and then produce an
output on the computer screen.
• Recall that, in Java the I/O is handled by objects called streams. Java
provides the following three standard streams as described as follows:
• Standard Input: This is used to feed the data to user's program and usually
a keyboard is used as standard input stream and represented as System.in.
• Standard Output: This is used to output the data produced by the user's
program and usually a computer screen is used for standard output stream
and represented as System.out.
• Standard Error: This is used to output the error data produced by the
user's program and usually a computer screen is used for standard error
stream and represented as System.err.
Java BufferedReader Class
• Java BufferedReader class is used to read the text from a character-
based input stream.
• It can be used to read data line by line by readLine() method.
• It makes the performance fast. It inherits Reader class.
• Let's see the declaration for Java.io.BufferedReader class:
public class BufferedReader extends Reader
Java BufferedReader class constructors
• The following table summarizes the constructors and their description
Java BufferedReader class methods
Opening a text file for reading
• A stream of the class BufferedReader is created and connected to a text file for
reading as follows:
BufferedReader streamName = new BufferedReader(new FileReader(filename));