首页javaswingworkerJava Swing - 如何监视InputStream的进度

Java Swing - 如何监视InputStream的进度

我们想知道如何监视InputStream的进度。
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);
    }
  }
}