Stream and File IO
Stream and File IO
Stream and File IO
1
Discuss on:
• What are the core reasons we use packages in
programming?
• What elements typically reside within a package?
• 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?
2
The Java File Class
• Java File class represents the files and directory pathnames in an
abstract manner.
• The File class is useful for retrieving information about files or
directories from disk.
• The File class contains the methods for obtaining the properties of
a file/directory and for renaming and deleting a file/directory.
• The File class has different methods which can be used to
manipulate the files.
– Absolute path: An absolute file name (or full name) contains a
file name with its complete path and drive letter.
– Relative path: A relative file name is in relation to the current
working directory.
3
The File Class
4
Example: The File Class
This program demonstrates how to create a File object and use the methods in the File
class to obtain its properties. The program creates a File object for the file us.gif.
The lastModified() method returns the date and time when the file was last modified. 5
Text File and Binary File
6
Example
• suppose you write the string "199" using text I/O to a file.
• 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.
7
1. Byte I/O Streams
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
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.
8
Text File Input and Output
• a File object encapsulates the properties of a file or a path but
does not contain the methods for reading/writing data from/to
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.
• To create an object using Scanner class is:
Scanner input = new Scanner(new File(filename));
11
PrintWriter Class
12
Example: wring data using PrintWriter
13
Example: reading data using Scanner
14
Stream
• Java uses the concept of a stream to make I/O operation fast.
– I/O means Input/Output.
• The java.io package contains all the classes required for input and
output operations.
• A stream can be defined as a sequence of data.
• An I/O Stream is a sequence of data that is read from a source and
write to a destination.
• All these streams represent an input source and an output
destination.
input output
Source Program Destination
stream stream
keyboard or file screen or file
15
Types I/O Streams
The abstract InputStream is the root class for reading byte data
and the abstract OutputStream is the root class for writing byte
data.
18
InputStream
The value returned is a byte as an int type.
19
OutputStream
20
FileInputStream/FileOutputStream
22
FileOutputStream
To construct a FileOutputStream, use the following constructors:
public FileOutputStream(String filename)
If the file already exists, the first two constructors would delete the
To retain the current content and append new data into the file, use
25
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.
26
DataInputStream/DataOutputStream
28
DataOutputStream
• DataOutputStream extends FilterOutputStream and implements
the DataOutput interface.
29
Characters and Strings in Byte 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.
• The writeBytes(String s) method writes the lower byte of the
Unicode for each character in the string s to the output. The high
byte of the Unicode is discarded.
• The writeBytes method is suitable for strings that consist of ASCII
characters, since an ASCII code is stored only in the lower byte of
a Unicode. If a string consists of non-ASCII characters, you have
to use the writeChars method to write the string.
30
Characters and Strings in Byte I/O
Why UTF-8? What is UTF-8?
• The writeUTF(String s) method writes two bytes of length information
to the output stream, followed by the modified UTF-8 representation of
every character in the string s.
• 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 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 0x7FFF are coded in two bytes. Other Unicode values are
coded in three bytes.
31
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)
• 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"));
34
BufferedInputStream/BufferedOutputStream
37
Constructing
BufferedInputStream/BufferedOutputStream
// Create a BufferedInputStream
// Create a BufferedOutputStream
38
BufferedInputStream/BufferedOutputStream
BufferedInputStream(new FileInputStream("temp.dat")));
39
2. Character I/O Streams
• Character Stream is an input and output data as a
sequence of characters.
• Java Byte streams are used to perform input and
output of 8-bit bytes, whereas Java Character
streams are used to perform input and output for
16-bit Unicode.
• All character stream classes are also descended
from two abstract classes Read and Writer.
• The most frequently used character stream
classes are: FileReader and FileWriter.
40
2. Character I/O Streams
41
3. Standard I/O 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.
• Java provides the following three standard streams:
– 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. 42
Read more on:
• Sub classes of other inputStream / outputStream
(e.g. ObjectInputStream/ObjectOutputStream
class)
• Sub classes of Reader and Writer Class
• Random access file
43
The End!!
44