Chapter-4 File I O
Chapter-4 File I O
METTU UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF COMPUTER SCIENCE
Java programming course
Chapter-4
Java Streams and File I/O
By: Naol G. 2
Introduction
• What is Stream.
• Streams: is connection (logical) that allows a sequence of data moving from program
to file or vice versa.
• Why IO stream
• In java, when we store data inside variables, they kept inside RAM (i.e. lost when
power off).
• EX.
class TemporaryDataStoring{
public static void main(String[] args) { Both, 200.45 and 1234 data is used
double balance = 200.45; only @execution time.
int rolno = 1234; • Then the data will be removed after
} program execution completed.
}
• Advantage of IO stream:
• Permanent data storage (On the disk, not on RAM)
By: Naol G. 3
• In Java, streams are the sequence of data that are read from the
source and written to the destination.
• An input stream allow java program to read data from the source (ex. File).
• An output stream allow java program to write data to the destination (ex. File).
More…
• getAbsolutePath() : String
• getAbsolutePath() returns the complete, non-relative path to the file.
• boolean f.delete() throws SecurityException
• Deletes the file.
• boolean f.renameTo(File f2) throws SecurityException
• Renames f to File f2. Returns true if successful.
•
By: Naol G. 10
By: Naol G. 11
Types of Streams
• Depending upon the type of data flow within the stream, it (stream)
can be classified into:
• Byte Stream
• Character Stream
• Note: both inputstream and Reader utilities for reading data from source to program.
• both outputstream and Writer utilities for writing data to file from program.
By: Naol G. 14
1) Byte Stream
• Byte stream is used to read and write a single byte (8 bits)
of data.
• All byte stream classes are derived from base abstract
classes called
• InputStream (to read) and OutputStream (to write).
By: Naol G. 15
• Subclasses of InputStream
• In order to use the functionality of InputStream, we can use its subclasses.
• Some of them are:
• FileInputStream
• ByteArrayInputStream
• ObjectInputStream
By: Naol G. 17
• Create an InputStream
• In order to create an InputStream, we must import the java.io.InputStream
package first.
• Once we import the package, here is how we can create the input stream.
Methods of InputStream
• The InputStream class provides different methods that are
implemented by its subclasses.
• Here are some of the commonly used methods:
By: Naol G. 19
Output
Available bytes in the file is: 49
Data read from the file:
This is a line of text inside the input.txt file.
By: Naol G. 20
• Subclasses of OutputStream
• In order to use the functionality of OutputStream, we can use its subclasses.
Some of them are:
• FileOutputStream
• ByteArrayOutputStream
• ObjectOutputStream
By: Naol G. 22
• Create an OutputStream
• In order to create OutputStream, we must import java.io.OutputStream package
first.
• Once we import the package, here is how we can create the output stream.
• Methods of OutputStream
• The OutputStream class provides different methods that are
implemented by its subclasses. Here are some of the methods:
By: Naol G. 24
2) Character Stream
• Character stream is used to read and write a single character of data.
• All the character stream classes are derived from base abstract
classes:
• Writer and
• Reader
By: Naol G. 26
• Subclasses of Writer
• In order to use the functionality of the Writer, we can use its subclasses.
Some of them are:
• BufferedWriter
• OutputStreamWriter
• FileWriter
• StringWriter
Output
By: Naol G. 28
• Create a Writer
• In order to create a Writer, we must import the java.io.Writer package first.
• Once we import the package, here is how we can create the writer.
• Here, we have created a writer named output using the FileWriter class.
• It is because the Writer is an abstract class.
• Hence we cannot create an object of Writer.
By: Naol G. 29
By: Naol G. 30
By: Naol G. 31
By: Naol G. 32
• Subclasses of Reader
• In order to use the functionality of Reader, we can use its subclasses.
• Some of them are:
• BufferedReader
• InputStreamReader
• FileReader
• StringReader
By: Naol G. 34
By: Naol G. 35
• Create a Reader
• In order to create a Reader, we must import the java.io.Reader package first.
• Once we import the package, here is how we can create the reader.
Output
End