public static boolean writeUserSelectedFile() {
      if (fileDialog == null)
         fileDialog = new JFileChooser();
      fileDialog.setDialogTitle("Select File for Output");
      File selectedFile;
      while (true) {
         int option = fileDialog.showSaveDialog(null);
         if (option != JFileChooser.APPROVE_OPTION)
            return false;  // user canceled
         selectedFile = fileDialog.getSelectedFile();
         if (selectedFile.exists()) {
            int response = JOptionPane.showConfirmDialog(null,
                  "The file \"" + selectedFile.getName() + "\" already exists.  Do you want to replace it?",
                  "Replace existing file?",
                  JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if (response == JOptionPane.YES_OPTION)
               break;
         }
         else {
            break;
         }
      }
      PrintWriter newout;
      try {
         newout = new PrintWriter(new FileWriter(selectedFile));
      }
      catch (Exception e) {
         throw new IllegalArgumentException("Can't open file \"" + selectedFile.getName() + "\" for output.\n"
                        + "(Error :" + e + ")");
      }
      if (!writingStandardOutput) {
         try {
            out.close();
         }
         catch (Exception e) {
         }
      }
      out = newout;
      writingStandardOutput = false;
      outputFileName = selectedFile.getName();
      outputErrorCount = 0;
      return true;
   }