3 Types of FILE IO in JAVA part1

TUTORIAL NO 10

3 Types of FILE IO part1

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 I am going to teach you how to read files in java using all the three types of methods available in java. The standard input output was the oldest after that new input output was used and now new input output is being used. To keep things simple we will simply read our program source code from the current directory using the three FILE IOs which will be selected by pressing 3 different buttons. In this tutorial we will be working in JAVA SWING. The first thing that we are going to do is setting up a JFrame and adding the required panels and Components. Basic step: Download and install ECLIPSE and set up a JAVA PROJECT. Then create a new class and name it FileIO. Then follow the steps 1.IMPORT STATEMENTS First of all write these import statements in your .java file
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.nio.*;
  6. import java.nio.file.*;
  7. import java.nio.channels.*;
  8. import java.util.EnumSet;
We require event import for mouse click , awt and swing imports for visual design, io import for standard input output, nio import for new input output and new input output 2. For this tutorial we will only be making two classes JPanel class and frame class. We will add the panel in the frame class 2. SETTING UP THE PANEL CLASS
  1. class Filepanel extends JPanel {
  2.  
  3.         JTextArea t_area = new JTextArea();
  4. JButton jb1 = new JButton("SIO");
  5.         JButton jb2 = new JButton("NIO");
  6.         JButton jb3 = new JButton("NIO2");
  7.         JLabel jl = new JLabel("File Name :");
  8.         JLabel rjl = new JLabel("Reading Time:");
  9.  
  10.         JTextField tf = new JTextField(30);
  11.         JTextField timetf = new JTextField(20);
  12.  
  13.         JScrollPane sp = new JScrollPane(t_area);
  14.         String file_name;
  15.         File myfile;
  16.  
  17.         long readingTime = 0, beforeReadingTime = 0, afterReadingTime = 0;
  18.         String path;// path of the code
  19.         String text = ""; // appending it into a string and then appending all the
  20.         char b[]; // for reading characters in file
First of all we will make the objects for adding text fields , labels and the text area for making the GUI as shown in the picture above. After that we need a scroll pane to add the scroll bars in the text area when necessary. Then we need to make a variable to store the file name and file reference for pointing towards the file and defining the path. Other than that we need character array for a reading the characters in the files. 3. WRITING THE CONTSTUCTOR
  1. Filepanel() {
  2.                 JPanel up = new JPanel();
  3.                 setLayout(new BorderLayout());
  4.  
  5.                 add(up, "North");
  6.                 up.add(jl);
  7.                 up.add(tf);
  8.                 up.add(rjl);
  9.                 up.add(timetf);
  10.                 up.add(jb1);
  11.                 up.add(jb2);
  12.                 up.add(jb3);
  13.  
  14.                 add(sp); // adding scroll panel
  15.  
  16.                 jb1.addMouseListener(new MouseAdapter() {
  17.                         public void mouseClicked(MouseEvent me) {
  18.                                 file_name = tf.getText();
  19.                                 if (me.getSource() == jb1)
  20.                                         sio();
  21.                         }
  22.                 });
  23.  
  24.                 jb2.addMouseListener(new MouseAdapter() {
  25.                         public void mouseClicked(MouseEvent me) {
  26.                                 //if (me.getSource() == jb2)
  27.                                         //nio();
  28.                         }
  29.                 });
  30.  
  31.                 jb3.addMouseListener(new MouseAdapter() {
  32.                         public void mouseClicked(MouseEvent me) {
  33.                                 //if (me.getSource() == jb3)
  34.                                         //nio2();
  35.                         }
  36.                 });
  37.         }// end constructor
In the constructor what we are doing is making a JPanel for adding in the north position and adding the labels and text fields and labels in that panel according to the GUI of the application. Then we are adding mouse listeners to the buttons so that they call the functions accordingly and read the a file by using the functionality of that function whether new or standard io.
  1. void computeReadTime() {
  2.                 readingTime = afterReadingTime - beforeReadingTime;
  3.                 timetf.setText("" + readingTime + "ns");
  4.         }
  5.  
  6. long getTime() {
  7.                 return System.nanoTime();
  8.         }
In this function we are simply calculating the time it took to read the file. For that we just need to subtract the after reading time from just before reading time (Remember: These variables will have stored values when we will call this function). Then we simply set the text field to that calculated time. After that we simply write another function in which simply returns the system time in nano seconds. We will use these functions in another function. 4. WRITING THE FUNCTION:
  1. void sio() {
  2.                 myfile = new File(file_name);
  3.                 t_area.setText(" ");
  4.                 text = "";
  5.                 if (myfile.exists()) { // if file is found
  6.                         beforeReadingTime = getTime();
  7.                         path = myfile.getPath();
  8.                         b = new char[(int) myfile.length()];
  9.                         try {
  10.                                 FileReader reader = new FileReader(path);
  11.                                 reader.read(b);
  12.                                 afterReadingTime = getTime();
  13.  
  14.                                 for (int i = 0; i < b.length; i++) {
  15.                                         text += b[i];   }
  16.                                 computeReadTime();
  17.                                 t_area.setText(text);
  18.                         } catch (FileNotFoundException e) {
  19.                         } catch (IOException e) {
  20.                         }
  21.                 }      
  22.                 else
  23.                 {
  24.                         text ="";
  25.                         t_area.setText(text);
  26.                         tf.setText("File Not Found.. !!");
  27.                 }
  28.         } // end sio
In this function first of all we clear any previous text written in the text area and then proceed. First of all we check whether the code file exists in the current directory or not if it does the first thing that we do is calculate the time just before the software is about to start reading. Then we set our character array size to the file size to read all the characters the file contains. Then we simply create a file reader reference to read the contents of the file and store in the character array. Then we will append all the characters in the text area to show the complete file contents. After that we simply calculate the time because we have read everything and written everything and we need to calculate the overall time it took to read from the file to writing in the text area. In the else condition we simply set the text to empty and write “file not found” in the text field. After that
  1. Void nio(){
  2. // next tutorial
  3. }
  4. Void nio2(){
  5. // next tutorial
  6. }
  7. }// end Filepanel
Now we will make the two functions for nio and nio2 which we will work on in the next tutorial. 5. WRITING THE MAIN PUBLIC CLASS
  1. public class FileIO extends JFrame {
  2.         static Filepanel fp = new Filepanel();
  3.         FileIO() {
  4.                 setDefaultCloseOperation(FileIO.EXIT_ON_CLOSE);
  5.                 setSize(1000, 700);
  6.                 add(fp);
  7.                 setVisible(true);
  8.         }
  9.         public static void main(String args[]) {
  10.                 new FileIO();
  11.         }// end main
  12. }// end file class
In the main frame class we first make a reference of the panel class to add in our frame class. In the constructor we declared the size of the frame and set it to visible and added the panel in the frame.In the main function we simply created an object of our frame class and our app is 40% complete. OUPUT: output
Tags

Add new comment