Chapter Four - File
Chapter Four - File
1 Compiled By Mikiyas D.
Outline
Introduction
Input Output Streams
Various stream classes
Using Streams
Object Streams
File Management
2 Compiled By Mikiyas D.
Introduction
Java I/O (Input and Output) is used to process the
input and produce the output.
Java uses the concept of a stream to make I/O operation
fast. The java.io package contains all the classes required
for input and output operations.
We can perform file handling in Java by Java I/O API.
3 Compiled By Mikiyas D.
What is Stream?
A stream is a sequence of data. In Java, a stream is composed of bytes.
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. It is also used read data
from a source.
System.in is an input stream
Output stream: a stream that accepts output from a program. It is also used for
writing data to its destination.
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
4 Compiled By Mikiyas D.
Various Stream Classes
Based on the data they handle there are two types of streams:
Byte Streams − These handle data in bytes (8 bits) i.e., the byte stream classes read/write
data of 8 bits. Using these you can store characters, videos, audios, images etc.
Character Streams − These handle data in 16 bit Unicode. Using these you can read and
write text data only.
In Java, java.io package contains InputStream, OutputStream, Reader class, Writer class
To access file
6 Compiled By Mikiyas D.
Standard Streams
In addition to above mentioned classes Java provides 3 standard
streams representing the input and, output devices
Standard Input − This is used to read data from user through input
devices. keyboard is used as standard input stream and represented as
System.in.
Standard Output − This is used to project data (results) to the user
through output devices. 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.
7 Compiled By Mikiyas D.
Using Streams – Byte Stream
OutputStream Class
8 Compiled By Mikiyas D.
Using Streams-FileOutputStream
FileOutputStream: is an output stream for writing data to a File.
It also belongs to byte streams classes, hence this class is meant for
writing streams of raw bytes in files.
Some Commonly Used Constructors:
9 Compiled By Mikiyas D.
Cont’d…….
2. public FileOutputStream(String name, boolean append) throws
FileNotFoundException
It is used to Creates a file output stream to write to the file with the
specified name.
If the second argument is true, then bytes will be written to the end of the
file rather than the beginning, i.e., the file will open in append mode rather
then overwrite mode.
If the file exists but is a directory does not exist and cannot be created, or
cannot be opened for any other reason then a FileNotFoundException is
thrown.
10 Compiled By Mikiyas D.
Cont’d…….
3. public FileOutputStream(File file) throws FileNotFoundException
This constructor creates a file output stream to write to the file
represented by the specified File object.
11 Compiled By Mikiyas D.
Using Streams-OutputStream Class
An output stream accepts output bytes and sends them to some
sink.
Useful methods of FileOutputStream
12 Compiled By Mikiyas D.
Using Streams-FileOutputStream (Write Byte)
13 Compiled By Mikiyas D.
Using Streams-FileOutputStream (Write String)
14 Compiled By Mikiyas D.
Using Streams-ByteArrayOutputStream
Java ByteArrayOutputStream class is used to write common data into multiple files.
In this stream, the data is written into a byte array which can be written to multiple
streams later.
The ByteArrayOutputStream holds a copy of data and forwards it to multiple
streams.
The buffer of ByteArrayOutputStream automatically grows according to data.
Some Useful Methods
15 Compiled By Mikiyas D.
Using Streams-ByteArrayOutputStream
16 Compiled By Mikiyas D.
Using Streams – Byte Stream
InputStream Class
17 Compiled By Mikiyas D.
Using Streams-InputStream Class
FileInputStream Class: Is an input stream to read data from the File.
It also belongs to byte streams classes category, hence this class is meant for
reading streams of raw bytes from files.
If the named file does not exist or for some other reason cannot be
18 Compiled By Mikiyas D.
Using Streams-InputStream Class
1. public FileInputStream(File file) throws FileNotFoundException
This constructor creates a FileInputStream by opening a connection to an
actual file, the file named by the File object file in the file system.
If the named file does not exist, or for some other reason cannot be
opened for reading then a FileNotFoundException is thrown.
19 Compiled By Mikiyas D.
FileInputStream Demo
Note: Before running the code, a text file named as “first.txt" is required to
be20created in a specified path. Compiled By Mikiyas D.
Using Streams-ByteArrayInputStream
The ByteArrayInputStream is composed of two words: ByteArray
and InputStream.
As the name suggests, it can be used to read byte array as input
stream.
Java ByteArrayInputStream class contains an internal buffer which
is used to read byte array as stream. In this stream, the data is
read from a byte array.
The buffer of ByteArrayInputStream automatically grows according
to data.
21 Compiled By Mikiyas D.
ByteArrayInputStream Demo
22 Compiled By Mikiyas D.
Using Streams-Character Stream
Character Stream
In addition to the byte based streams, Java provides Reader and
Writer classes which operates based on the Unicode (two-byte
character) streams. Means that
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.
Both Reader and Writer are abstract classes for character streams,
as well as there are many classes related to character streams but the
most
23
frequently used classesCompiled
are, FileReader
By Mikiyas D.
and FileWriter.
Using Streams-Character Stream
Most of the byte-based streams have corresponding character
based Reader or Writer classes.
However, internally FileReader uses FileInputStream and FileWriter
uses FileOutputStream but here the major difference is that
FileReader reads two bytes at a time and FileWriter writes two
bytes at a time.
Character-based streams use Unicode characters such streams can
process data in any language that the Unicode character set
represents.
24 Compiled By Mikiyas D.
Using Streams – Character Stream
Writer Class
25 Compiled By Mikiyas D.
Using Streams-Writer Class
It is an abstract class for writing to character streams.
The methods that a subclass must implement are write(char[], int,
int), flush(), and close().
Most subclasses will override some of the methods defined here to
provide higher efficiency, functionality or both.
26 Compiled By Mikiyas D.
Using Streams-Writer Class
Some Methods Used:
27 Compiled By Mikiyas D.
Writer Class Demo
28 Compiled By Mikiyas D.
Using Streams-FileWriter Class
Java FileWriter class is used to write character-oriented data to a file.
It is character-oriented class which is used for file handling in java.
Unlike FileOutputStream class, you don't need to convert string into
byte array because it provides method to write string directly.
Constructors Used:
29 Compiled By Mikiyas D.
FileWriter Demo
30 Compiled By Mikiyas D.
Using Streams – Character Stream
Reader Class
31 Compiled By Mikiyas D.
Using Streams-Reader Class
Java Reader is an abstract class for reading
character streams.
The only methods that a subclass must implement are
read(char[], int, int) and close().
Most subclasses, however, will override some of the methods
to provide higher efficiency, additional functionality, or both.
Some of the implementation class are BufferedReader,
CharArrayReader, FilterReader, InputStreamReader,
PipedReader, StringReader.
32 Compiled By Mikiyas D.
Using Streams-Reader Class
33 Compiled By Mikiyas D.
Reader Demo
34 Compiled By Mikiyas D.
Using Streams-FileReader Class
Java FileReader class is used to read data from the file.
It returns data in byte format like FileInputStream class.
It is character-oriented class which is used for file handling in java.
35 Compiled By Mikiyas D.
FileReader Demo
36 Compiled By Mikiyas D.
File Management
The most used class in Java input/output is File class.
The File class in the Java IO API gives you access to the underlying
file system.
Using the File class you can:
Check if a file or directory exists.
Create a directory if it does not exist.
Rename or move a file.
Create and/or Delete a file.
Check if path is file or directory.
Read list of files in a directory.
37 Compiled By Mikiyas D.
Cont’d……..
Some Important methods of File class:
38 Compiled By Mikiyas D.
File Management Demo
39 Compiled By Mikiyas D.
Cont’d…..
40 Compiled By Mikiyas D.
41
?
Compiled By Mikiyas D.