COP 3337 SECTION U02 FALL 2012 THE FILE CHOOSER PROJECT ======================== This program illustrates the use of the JFileChooser to navigate through a directory. I. Below is the program: import javax.swing.JFileChooser; import java.io.*; import java.util.Scanner; /** * * @author pelina */ public class Main { public static void main(String[] args) { System.out.println("Input With The JFile Chooser"); System.out.println("============================\n\n\n"); System.out.println("Read a file and copy it, one word per line."); // form a file chooser object JFileChooser chooser = new JFileChooser(); // get the input file File in = null; int status = chooser.showOpenDialog(null); if (status == JFileChooser.APPROVE_OPTION) { in = chooser.getSelectedFile(); String name = in.getPath(); System.out.println("You selected " + name); } // form the output file System.out.println("Enter the name of the output file."); Scanner keyboard = new Scanner(System.in); String outName = keyboard.next(); try { FileWriter fWriter = new FileWriter(outName); PrintWriter out = new PrintWriter(fWriter); // copy the input file Scanner inFile = new Scanner(in); while (inFile.hasNext()) { String current = inFile.next(); out.println(current); } out.close(); } catch(IOException exception) { System.out.println("Error processing file: " + exception); } } } II. Here is the output of the run: Input With The JFile Chooser ============================ Read a file and copy it, one word per line. You selected U:\public_html\COP3337\message.txt Enter the name of the output file. U:\\public_html\\COP3337\detailedMessage.txt BUILD SUCCESSFUL (total time: 1 minute 34 seconds) III. The file U:\public_html\COP3337\message.txt is between the dash lines. -------------------------------------------------------------------------- I really love COP2210 and its teachers, the Geezer, Father Norman, and Papa Bill. -------------------------------------------------------------------------- IV. The file U:\public_html\COP3337\detailedMessage.txt is between the dash lines. -------------------------------------------------------------------------- I really love COP2210 and its teachers, the Geezer, Father Norman, and Papa Bill. --------------------------------------------------------------------------