Reading a File with a Progress Monitor in Java

This tutorial will teach you how to create a program that can read files but with a progress monitor that loads first the file and then read it. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of progressMonitorInputStream.java. 2. Import the following package library:
  1. import java.io.*; // used to access FileInputStream, InputStreamReader, and BufferedReader class
  2. import javax.swing.*; // used to access ProgressMonitorInputStream clas
3. We will have first to have a try and catch method. In the try method, initialize the following variables:
  1.     try {
  2.       FileInputStream fis = new FileInputStream("READ FIRST.txt"); // access your file in the file input stream
  3.       ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(null, "Reading the file..", fis); // this will load and monitor the progress of the file
  4.       InputStreamReader isr = new InputStreamReader(pmis); //used to access the progress monitor when reading the file
  5.       BufferedReader br = new BufferedReader(isr); //this will buffer or load the input stream
  6.       String line; //this variable containes the content of the file
Next, to access the content of the file, we will use the while loop and have the contents of the file be put into the line variable. Then close the file with your br variable.
  1.       while ((line = br.readLine()) != null) {
  2.         System.out.println(line);
  3.       }
  4.       br.close();
  5.     }
4. In your catch method, get the following message when you have an error. Then provide an exit for your program if there is an error.
  1.     } catch (Exception exception) {
  2.      
  3.       System.out.println(exception.getMessage());
  4.     }
  5.     System.exit(2);
Output: output Here's the full code of this tutorial:
  1. import java.io.*; // used to access FileInputStream, InputStreamReader, and BufferedReader class
  2. import javax.swing.*; // used to access ProgressMonitorInputStream clas
  3.  
  4. public class progressMonitorInputStream {
  5.  
  6.   public static void main(String args[]) {
  7.    
  8.     try {
  9.       FileInputStream fis = new FileInputStream("READ FIRST.txt"); // access your file in the file input stream
  10.       ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(null, "Reading the file..", fis); // this will load and monitor the progress of the file
  11.       InputStreamReader isr = new InputStreamReader(pmis); //used to access the progress monitor when reading the file
  12.       BufferedReader br = new BufferedReader(isr); //this will buffer or load the input stream
  13.       String line; //this variable containes the content of the file
  14.      
  15.      
  16.       while ((line = br.readLine()) != null) {
  17.         System.out.println(line);
  18.       }
  19.       br.close();
  20.     } catch (Exception exception) {
  21.      
  22.       System.out.println(exception.getMessage());
  23.     }
  24.     System.exit(2);
  25.   }
  26. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment