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
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:


  1. import java.io.*;
  2. public class TestNodeStreams
  3. {
  4. public static void main(String args[])
  5. {
  6. try{
  7. FileReader input = new FileReader(args[0]);
  8. try{
  9. FileWritere output = new FileWriter(args[1]);
  10. try{
  11. chat[] buffer = new char[128];
  12. int charsRead;
  13. // read the first buffer
  14. charsRead = input.read(buffer);
  15. while(chars!=-1)
  16. // write buffer to the output file
  17. Output.write(buffer,0,charsRead);
  18. // read the next buffer
  19. charsRead = input.read(buffer);
  20. }finally
  21. {
  22. output.close();
  23. }
  24. }finally{
  25. input.close();
  26. }
  27. }catch(IOException e)
  28. {
  29. e.printStackTrace();
  30. }
  31. }
  32. }
  • 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.

java I/O Fundamentals

Buffered Stream:-

The code given below performs the same function as the program in above code but uses BufferedReader and BufferedWriter.

  1. import java.io.*;
  2. public class TestBufferedStreams
  3. {
  4. public static void main(String args[])
  5. {
  6. try{
  7. FileReader input = new FileReader(args[0]);
  8. BufferedReader bufInput = new BufferedReader(input);
  9. try{
  10. FileWritere output = new FileWriter(args[1]);
  11. BufferedWriter bufOutput = new BufferedWriter(Output);
  12. try{
  13. String line;
  14. // read the first line
  15. line = bufInput.readLine();
  16. while(line!=null)
  17. // write the line out to the output file
  18. bufOutput.write(line,0,line.length());
  19. bufOutput.readLine();
  20. // read the next line
  21. line = bufInput.readLine();
  22. }finally
  23. {
  24. bufOutput.close();
  25. }
  26. }finally{
  27. bufInput.close();
  28. }
  29. }catch(IOException e)
  30. {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
  • 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.

Sourabh Bhunje

Sourabh Bhunje, B.E. IT from Pune University. Currently Working at Techliebe. Professional Skills: Programming - Software & Mobile, Web & Graphic Design, Localization, Content Writing, Sub-Titling etc. http://techliebe.com/about-us

Leave a Reply