ProgressMonitorInputStream Demo : Data Input Output : File Input Output : Java examples (example source code) Organized by topic

Java
C++
Java Home »  File Input Output   » [  Data Input Output  ]  Screenshots 
 



ProgressMonitorInputStream Demo

/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski     
ISBN: 1-893115-78-X
Publisher: APress
*/

import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;

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;
    if (args.length != 1) {
      System.err.println("Usage:");
      System.err.println("java ProgressInput filename");
    else {
      try {
        FileInputStream fis = new FileInputStream(args[0]);
        JLabel filenameLabel = new JLabel(args[0], JLabel.RIGHT);
        filenameLabel.setForeground(Color.black);
        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);
          try {
            Thread.sleep(500);
          catch (Exception e) {
          }
        }
        br.close();
      catch (FileNotFoundException exception) {
        System.err.println("Bad File " + exception);
        returnValue = BAD_FILE;
      catch (InterruptedIOException exception) {
        System.err.println("Canceled");
        returnValue = CANCELED;
      catch (IOException exception) {
        System.err.println("I/O Exception " + exception);
        returnValue = PROBLEM;
      }
    }
    // AWT Thread created - must exit
    System.exit(returnValue);
  }
}

           
       
Related examples in the same category
1.  Data IO Test 2 Data IO Test 2
2.  Data IO Demo Data IO Demo
3.  Data IO Test Data IO Test
4.  Typical I/O stream configurations
5.  IO demo: DataOutputStream and DataInputStream
6.  Some simple file I-O primitives reimplemented in Java
7.  ScanStreamTok - show scanning a file with StringTokenizer
8.  Write some data in binary
9.  Using transferTo() between channels
10.  Controlling serialization by adding your own writeObject() and readObject() methods Controlling serialization by adding your own writeObject() and readObject() methods
11.  Read Write Lock Test Read Write Lock Test








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.