1  import java.io.File;
  2  import java.io.IOException;
  3  import java.util.Scanner;
  4  import javax.swing.JFileChooser;
  5  
  6  /**
  7     This program shows the first nine lines of a file that was
  8     selected with a file chooser.
  9  */
 10  public class FileChooserDemo 
 11  {
 12     public static void main(String[] args) throws IOException
 13     {
 14        JFileChooser chooser = new JFileChooser();
 15        Scanner in = null;
 16        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
 17        {
 18           File selectedFile = chooser.getSelectedFile();
 19           in = new Scanner(selectedFile);
 20           int lineNumber = 1;
 21           final int MAX_LINES = 9;
 22           while (in.hasNextLine() && lineNumber <= MAX_LINES)
 23           {
 24              String line = in.nextLine();
 25              System.out.println(lineNumber + ": " + line);
 26              lineNumber++;
 27           }
 28           if (in.hasNextLine()) { System.out.println("..."); }
 29        }
 30     }
 31  }