I/O Fundamentals in JAVA 3
Node Stream:-
In the java JDK, there are three fundamental types of nodes,
- Files
- Memory (such a arrays or String objects)
- Pipes (a channel from one process or thread [a light-weight process] to another; the output pipe stream of one thread is attached to the input pipe stream of another thread)
It is possible to create new node stream classes, but it requires handling native function calls to device driver, and this is non portable.
Below shows the node streams:
Type | Character Streams | Byte Streams |
File | FileReader | FileInputStream |
FileWriter | FileOutputStream | |
Memory Array | CharArrayReader | ByteArrayInputStream |
CharArrayWriter | ByteArrayOutputStream | |
Memory String | StringReader | N/A |
StringWriter | ||
Pipe | PipedReader | PipedInputStream |
PipedWriter | PipedOutputStream |
A simple Example:-
- The code given below reads character from a file named by the first command line argument and writes the character out to a file named by the second command line argument.
- Thus, it copies the file. This is how the program might be invoked:
- import java.io.*;
- public class TestNodeStreams
- {
- public static void main(String args[])
- {
- try{
- FileReader input = new FileReader(args[0]);
- try{
- FileWritere output = new FileWriter(args[1]);
- try{
- chat[] buffer = new char[128];
- int charsRead;
- // read the first buffer
- charsRead = input.read(buffer);
- while(chars!=-1)
- // write buffer to the output file
- Output.write(buffer,0,charsRead);
- // read the next buffer
- charsRead = input.read(buffer);
- }finally
- {
- output.close();
- }
- }finally{
- input.close();
- }
- }catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- As easy as this was, handling the buffer is tedious and error prone. It turns out that there are classes that handle the buffering for you and present you with the capability to read a stream a line at a time.
- It is called a BufferedReader and is a type of a stream called a processing stream.
Buffered Stream:-
The code given below performs the same function as the program in above code but uses BufferedReader and BufferedWriter.
- import java.io.*;
- public class TestBufferedStreams
- {
- public static void main(String args[])
- {
- try{
- FileReader input = new FileReader(args[0]);
- BufferedReader bufInput = new BufferedReader(input);
- try{
- FileWritere output = new FileWriter(args[1]);
- BufferedWriter bufOutput = new BufferedWriter(Output);
- try{
- String line;
- // read the first line
- line = bufInput.readLine();
- while(line!=null)
- // write the line out to the output file
- bufOutput.write(line,0,line.length());
- bufOutput.readLine();
- // read the next line
- line = bufInput.readLine();
- }finally
- {
- bufOutput.close();
- }
- }finally{
- bufInput.close();
- }
- }catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- The flow of this program is the same as before. Instead of reading a buffer, this program reads a line at a time using the line variable to hold the String returned by the readLine method (Line 18 and Line 24), which provides greater efficiency.
- Line 11 chains the file reader object within a buffered reader stream. You manipulate the outer most stream in the chain (bufInput), which manipulates the inner most stream (input).
For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.