FileDocCategorySizeDatePackage
TextChooser.javaAPI DocExample1561Sun Mar 28 19:08:42 BST 1999None

TextChooser.java

import java.io.*;
import java.awt.*;
import com.macfaq.io.*;


public class TextChooser implements FilenameFilter {

  public static void main(String[] args) {
  
    try {
      File f = getFile();
      if (f == null) return;
      FileInputStream fin = new FileInputStream(f);
      StreamCopier.copy(fin, System.out);
    }
    catch (IOException e) {
      System.err.println(e);
    }    
  
    // Work around annoying AWT non-daemon thread bug
    System.exit(0);
  
  }

  public boolean accept(File dir, String name) {
 
    if (name.endsWith(".java")) return true;
    else if (name.endsWith(".jav")) return true;
    else if (name.endsWith(".html")) return true;
    else if (name.endsWith(".htm")) return true;
    else if (name.endsWith(".txt")) return true;
    else if (name.endsWith(".text")) return true;
    return false;
 
  }

  public static File getFile() throws IOException {
  
    // dummy Frame, never shown
    Frame parent = new Frame();
    FileDialog fd = new FileDialog(parent, "Please choose a file:", 
     FileDialog.LOAD);
    fd.setFilenameFilter(new TextChooser());
    fd.show();

    // program stops here until user selects a file or cancels
    
    String dir = fd.getDirectory();
    String file = fd.getFile();
    
    // clean up our windows, they won't be needed again
    parent.dispose();
    fd.dispose();
    
    if (dir == null || file == null) { // user cancelled the dialog
      return null;
    }
    return new File(dir, file);
    
  }

}