Java Tutorial/Swing/ProgressMonitorInputStream

Материал из Java эксперт
Перейти к: навигация, поиск

14. ProgressMonitorInputStream

Represent an input stream filter that uses a ProgressMonitor to check the progress of the reading of an input stream.



import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.JLabel;
import javax.swing.ProgressMonitorInputStream;
public class ProgressInputSample {
  public static final int NORMAL = 0;
  public static final int BAD_FILE = 1;
  public static final int CANCELED = NORMAL;
  public static final int PROBLEM = 2;
  public static void main(String args[]) {
    int returnValue = NORMAL;
    try {
      FileInputStream fis = new FileInputStream("person.xml");
      JLabel filenameLabel = new JLabel("persion.xml", JLabel.RIGHT);
      Object message[] = { "Reading:", filenameLabel };
      ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(null, message, fis);
      InputStreamReader isr = new InputStreamReader(pmis);
      BufferedReader br = new BufferedReader(isr);
      String line;
      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
      br.close();
    } catch (Exception exception) {
      returnValue = PROBLEM;
    }
    System.exit(returnValue);
  }
}