Unit VII - Java File Input/Output
Java IO Stream
Java performs I/O through Streams. A Stream is linked to a physical layer by java I/O system to make input and output operation in java. In general, a stream means continuous flow of data. Streams are clean way to deal with input/output without having every part of your code understand the physical.
Java encapsulates Stream under java.io package. Java defines two types of streams. They are,
- Byte Stream : It provides a convenient means for handling input and output of byte.
- Character Stream : It provides a convenient means for handling input and output of characters. Character stream uses Unicode and therefore can be internationalized.
Java Byte Stream Classes
Byte stream is defined by using two abstract class at the top of hierarchy, they are InputStream and OutputStream.
These two abstract classes have several concrete classes that handle various devices such as disk files, network connection etc.
Some important Byte stream classes.
Stream class | Description |
---|---|
BufferedInputStream | Used for Buffered Input Stream. |
BufferedOutputStream | Used for Buffered Output Stream. |
DataInputStream | Contains method for reading java standard datatype |
DataOutputStream | An output stream that contain method for writing java standard data type |
FileInputStream | Input stream that reads from a file |
FileOutputStream | Output stream that write to a file. |
InputStream | Abstract class that describe stream input. |
OutputStream | Abstract class that describe stream output. |
PrintStream | Output Stream that contain print() and println() method |
These classes define several key methods. Two most important are
read()
: reads byte of data.write()
: Writes byte of data.
Java Character Stream Classes
Character stream is also defined by using two abstract class at the top of hierarchy, they are Reader and Writer.
These two abstract classes have several concrete classes that handle unicode character.
Some important Charcter stream classes
Stream class | Description |
---|---|
BufferedReader | Handles buffered input stream. |
BufferedWriter | Handles buffered output stream. |
FileReader | Input stream that reads from file. |
FileWriter | Output stream that writes to file. |
InputStreamReader | Input stream that translate byte to character |
OutputStreamReader | Output stream that translate character to byte. |
PrintWriter | Output Stream that contain print() and println() method. |
Reader | Abstract class that define character stream input |
Writer | Abstract class that define character stream output |
Reading Console Input
We use the object of BufferedReader class to take inputs from the keyboard.
Reading Characters
read()
method is used with BufferedReader object to read characters. As this function returns integer type value has we need to use typecasting to convert it into char type.
Below is a simple example explaining character input.
Reading Strings in Java
To read string we have to use readLine()
function with BufferedReader class's object.
Comments
Post a Comment