Scanner and JFileChooser

By: Nicholas Duchon

Outline:



JFileChooser

Here's an example using JFileChooser:

// File: ChooserExample.java
// Date: Apr 30, 2010
// Author: Nicholas Duchon
// Purpose: Demonstrate JFileChooser
//    reading a file with Scanner
//    starting chooser in current directory

 import java.io.FileNotFoundException;
 import javax.swing.JFileChooser;
 import javax.swing.JOptionPane;
 import java.util.Scanner;

 public class ChooserExample {
    public static void main (String args []) {
       // sometimes starting with JFileChooser can be erratic,
       // so the following seems to be a reliable way to start:
       JOptionPane.showMessageDialog(null, "Welcome.\nBy: Nicholas Duchon\nOct 12, 2017");
       // start the search in the current directory
       JFileChooser chooser = new JFileChooser(".");
       int returnVal = chooser.showOpenDialog(null);
       if(returnVal == JFileChooser.APPROVE_OPTION) {
          System.out.println("You chose to open this file: " +
             chooser.getSelectedFile().getName());
          // open and read file:
          try {
             Scanner sfin = new Scanner (chooser.getSelectedFile()); // NOT the file name!!!!
             while (sfin.hasNext())
                System.out.println (sfin.nextLine());
          }
             catch (FileNotFoundException e) {
                JOptionPane.showMessageDialog(null, "File not found.");
             } // end try/catch
       } // end if file chosen
    } // end main
 } // end file ChooserExample


Pestering

Here's some very simple code to open a file, pestering the user until the open succeeds.

NOTE: The "FileNotFoundException" is thrown by the Scanner constructor, NOT by the File constructor!

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;

public class GetFile {
public static void main (String args []) {
Scanner sin = new Scanner (System.in);
Scanner fin = null;
String fname = "";

while (fin == null) {
System.out.print ("File name: ");
fname = sin.next(); // get file name
try {
fin = new Scanner (new File (fname));
} catch (FileNotFoundException e) {
System.out.println ("File not found: " + fname);
}
} // end until good file found

System.out.println ("File open ok: " + fname);
} // end main

} // end class GetFile

Filtering

Example using JFileChooser with file name extension filtering
// File: ChooserExample.java
// Date: Apr 30, 2010
// Author: Nicholas Duchon
// Purpose: Demonstrate JFileChooser
//    reading a file with Scanner
//    starting chooser in current directory


   import java.io.FileNotFoundException;
   import javax.swing.JFileChooser;
   import javax.swing.filechooser.FileNameExtensionFilter;
   import javax.swing.JOptionPane;
   import java.util.Scanner;


   public class ChooserExample {
      public static void main (String args []) {
         Scanner sfin = getScanner (); // call method to select and open file
         while (sfin.hasNext()) 
            System.out.println (sfin.nextLine());
      } // end main
    
      public static Scanner getScanner () {
       // start the search in the current directory
         JFileChooser chooser = new JFileChooser("."); // Start selection at dot!

         FileNameExtensionFilter filter;
         filter = new FileNameExtensionFilter("txt", "txt");
         chooser.setFileFilter(filter);
         chooser.addChoosableFileFilter (new FileNameExtensionFilter("java", "java"));
         chooser.addChoosableFileFilter (new FileNameExtensionFilter("txt, java and class", "txt", "java", "class"));
      
         int returnVal = chooser.showOpenDialog(null);
         if(returnVal == JFileChooser.APPROVE_OPTION) {
            System.out.println("You chose to open this file: " +
               chooser.getSelectedFile().getName());
          // attach Scanner to open and read file:
            try {
               return new Scanner (chooser.getSelectedFile());
            } 
               catch (FileNotFoundException e) {
                  JOptionPane.showMessageDialog(null, "File not found.");
               } // end try/catch
         } // end if file chosen
         return null;
      } // end method getScanner
   
   } // end file ChooserExample


Delimiters

Using regular expressions to define delimiters in Scanners:


ND.