Ambo University Woliso campus
Department of Computer science
Advanced Programming
Instructor Name: Lencho J
February 23,2022
Chapter 4: Streams and File I/O
• A stream is a sequence of bytes characters that flows from a
source to a destination
• In a program, we read information from an input stream and write
information to an output stream
• A program can manage multiple streams at a time
• The java.io package contains many classes that allow us to define
various streams with specific characteristics
I/o Stream(cont’d…)
• 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.
• They are designed to be read by programs.
• For example, Java source programs are stored in text files and can be
read by a text editor, but 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.
I/o Stream(cont’d…)
Stream classes
• InputStream and OutputStream are designed for byte streams.
• Reader and Writer are designed for character streams.
• The byte stream classes and the character stream classes form
separate hierarchies.
• In general, you should use the character stream classes when
working with characters or strings and use the byte stream classes
when working with bytes or other binary objects.
The Character Streams
• While the byte stream classes provide sufficient functionality to
handle any type of I/O operation, they cannot work directly with
Unicode characters.
• It was necessary to include direct I/O support for characters.
• Character streams are defined by using two class hierarchies.
• At the top are two abstract classes: Reader for reading
characters and Writer for writing characters.
Predefined Streams
• As you know, all Java programs automatically import the java.lang
package.
• This package defines a class called System, which encapsulates
several aspects of the run-time environment.
• System contains three predefined stream variables: in, out, and err.
These fields are declared as public, static, and final within System.
• These variables can be used by any other part of your program and
without reference to a specific System object.
• System.out refers to the standard output stream. By default, this is the
console.
• System.in refers to standard input, which is the keyboard by default.
Reading Console Input
• In Java 1.0, the only way to perform console input was to use a byte stream.
• Using a byte stream to read console input is still acceptable. However, for commercial
applications, the preferred method of reading console input is to use a character
oriented stream.
• In Java, console input is accomplished by reading from System.in.
• To obtain a character-based stream that is attached to the console, wrap System.in in a
BufferedReader object.
• To read a character from a BufferedReader, use read( ) method
• Each time that read( ) is called, it reads a character from the input stream and returns it
as an integer value.
• To read a string from the keyboard, use the version of readLine( ) that is a member of
the BufferedReader class.
Reading Console Input
Reading String from console input
Text files
• Text (.txt) files are the simplest kind of files
• Text files can be used by many different programs
• Formatted text files (such as .doc files) also contain binary
formatting information
• Only programs that “know the secret code” can make sense
of formatted text files
• Compilers, in general, work only with text
Reading and Writing Files
FileWriter
• It creates a Writer that you can use to write to a file
• Creation of a FileWriter is not dependent on the file already
existing.
• FileWriter will create the file before opening it for output, when
you create the object.
• In the case where you attempt to open a read-only file, an
IOException will be thrown.
Methods of File Writer
• Write(char[] ch):
write array of characters to the file
• Write(string s):
used to write string to the file.
• Flush():
to give the guarantee that total data including last character
written properly to the file.
• Close():
used to close the file
Directories in Java
• A directory is a File which can contains a list of other files and directories.
• You use File object to create directories, to list down files available in a
directory.
• There are two useful File utility methods, which can be used to create
directories:
• The mkdir method creates a directory, returning true on success and false
on failure.
• Failure indicates that the path specified in the File object already exists,
or that the directory cannot be created because the entire path does not
exist yet.
• The mkdirs method creates both a directory and all the parents of the
directory.
Reading File
FileReader
• The File Reader class creates a Reader that you can
use to read the contents of a file.
• Its two most commonly used constructors are shown
here:
FileReader(String filePath)
FileReader(File fileObj)
Reading File(cont’d…)
• BufferedReader is a subclass of Reader
• It buffers the character stream from FileReader and
has readLine() method to read an entire line of
characters efficiently
FileReader fr = new FileReader("myFile.txt");
BufferedReader br = new BufferedReader(fr);