3 Types of FILE IO part2

TUTORIAL NO 10

3 Types of FILE IO part2

In this tutorial you will learn: File Reading using 1.Standard Input Output 2.New Input Output 3.New Input Output2 4.Anonymous classes Today we are going to write our remaining two filing functions i.e of new input output and new input output 2. If you haven’t followed my previous tutorial go through it first using this link http://www.sourcecodester.com/tutorials/java/7513/3-types-file-io-java-part1.html Basic step: Follow the previous tutorial and write the complete program after that continue from here: 1.IMPORT STATEMENTS First of all write these import statements in your .java file
  1. import java.nio.*;
  2. import java.nio.file.*;
  3. import java.nio.channels.*;
  4. import java.util.EnumSet;
We require nio imports for new input output, nio.channels import for new Input output 2 and enumSet import for some functions of new input output 2 2. SETTING UP NIO FUNCTION:
  1. void nio() {
  2.        
  3.                 file_name = tf.getText();
  4.                 Path myfile = Paths.get(file_name);
  5.                 if(Files.exists(myfile)){
  6.                         beforeReadingTime = getTime();
  7.                 try {
  8.                         text = "";
  9.                         byte b[] = Files.readAllBytes(myfile);
  10.                         afterReadingTime = getTime();
First of all we will check whether the file exists in our current directory or not. We will do this by passing the path parameter to exists function. Remember we need to call it with a class named Files class because it is a static function of that class. Point to remember is that in new input output we don’t make a reference of the file class we simply make the reference of the path class so we need a class to handle the functions of the file so there is a Files class for that. Now we start our try catch block first of all we empty our text string. Then make a byte array and store all the bytes read from the file using the readAllbytes function which is called using the Files class name which indicates that it is also a static function. After that we simply calculated our after reading time to set the time in text area.
  1. for (int i = 0; i < b.length; i++) {
  2.                                 text += (char) b[i];
  3.                         }
  4.                         computeReadTime();
  5.                         t_area.setText(text);
  6.                 } catch (IOException e) {}
  7.                 }
  8.                 else
  9.                 {
  10.                         text ="";
  11.                         t_area.setText(text);
  12.                         tf.setText("File Not Found.. !!");
  13.                 }
  14.         } // end nio
After reading the file we need to add the read contents to the text so we simply add the characters of the file to the text area. Then we ended our try blocks followed by the catch blocks to catch the IOException. If the file not found we simply set the message in the text area. 3. WRITING NIO2 FUNCTION:
  1. void nio2() {
  2.                 file_name = tf.getText();
  3.                 Path myfile = Paths.get(file_name);
  4.                 text = "";
  5.                 int i = 0;
  6.                 if (Files.exists(myfile)) {
  7.                         beforeReadingTime = getTime();
In this function like always we first clear the text area then specify the file path after that we check if the files exist in the current directory after we compute the reading time because we are just about to read the file.
  1. try {
  2.                 ByteBuffer bf;
  3.                 bf = ByteBuffer.allocate((int) Files.size(myfile));
  4.  
  5.                 ReadableByteChannel rbc = Files.newByteChannel(myfile);
  6.                 text = "";
  7.                 while (rbc.read(bf) != -1 & i < Files.size(myfile)) {
  8.                         byte b[] = bf.array();
  9.                         text += (char) b[i];
  10.                         i++;
  11.                 }
  12.                 afterReadingTime = getTime();
  13.                 computeReadTime();
  14.                 t_area.setText(text);
  15.         } catch (IOException e) {}
Now for new input output 2 we first created a file buffer and then allocate the size of the file length( Remember: If you allocate small size then remember to flip after reading the contents).Then we create a readablebytechannel and set the file path for that file channel. After that we empty the string and start reading the contents of the file using the byte channel and buffer. We store the values of the buffer in the byte array and convert it to character then add it to the string text variable. After all that we simply computed the reading time and set the text area.
  1. else
  2. {
  3.         text ="";
  4.         t_area.setText(text);
  5.         tf.setText("File Not Found.. !!");
  6.                 }
  7.         }// end nio2
  8. }// end Filepanel
In the else we simply set the text field to file not found. Now are application is complete and our all the button will be working with three different methods of reading the file. OUTPUT: output

Add new comment