Java Swing How to - Monitor Progress of InputStream








Question

We would like to know how to monitor Progress of InputStream.

Answer

/*from  www  . j  a  v a 2 s .c o m*/
import java.io.FileInputStream;

import javax.swing.JOptionPane;
import javax.swing.ProgressMonitorInputStream;

public class Main {

  public static void main(String args[]) {
    ProgressMonitorInputStream monitor;
    try {
      monitor = new ProgressMonitorInputStream(null, "Loading ",
          new FileInputStream("yourFile.dat"));
      while (monitor.available() > 0) {
        byte[] data = new byte[38];
        monitor.read(data);
        System.out.write(data);
      }
    } catch (Exception e) {
      JOptionPane.showMessageDialog(null, "Unable to find file: yourFile.dat",
          "Error", JOptionPane.ERROR_MESSAGE);
    }
  }
}