import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FileChooser extends JPanel implements ActionListener {
 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 public static void main(String[] args) {
  // Schedule a job for the event dispatch thread:
  // creating and showing this application's GUI.
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    // Turn off metal's use of bold fonts
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    createAndShowGUI();
   }
  });
 }
 static private final String newline = "\n";
 static public File file = null;
 JButton openButton, checkButton, loadToExcel, Clear;
 JTextArea log;
 JFileChooser fc;
 JTextField textField;
 ArrayList msgstorer;
 boolean foundFlag = false;
 String text = null;
 @SuppressWarnings("deprecation")
 public FileChooser() {
  super(new BorderLayout());
  // Create the log first, because the action listeners
  // need to refer to it.
  log = new JTextArea(5, 20);
  log.setMargin(new Insets(5, 5, 5, 5));
  log.setEditable(false);
  JScrollPane logScrollPane = new JScrollPane(log);
  // Create a file chooser
  fc = new JFileChooser();
  // Uncomment one of the following lines to try a different
  // file selection mode. The first allows just directories
  // to be selected (and, at least in the Java look and feel,
  // shown). The second allows both files and directories
  // to be selected. If you leave these lines commented out,
  // then the default mode (FILES_ONLY) will be used.
  //
  fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
  // fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  // Create the open button. We use the image from the JLF
  // Graphics Repository (but we extracted it from the jar).
  openButton = new JButton("Open a Folder...",
    createImageIcon("images/open1.gif"));
  openButton.addActionListener(this);
  // Create the save button. We use the image from the JLF
  // Graphics Repository (but we extracted it from the jar).
  checkButton = new JButton("Check  the Files...",
    createImageIcon("images/check1.gif"));
  checkButton.addActionListener(this);
  textField = new JTextField(50);
  loadToExcel = new JButton("Load to CSV",
    createImageIcon("images/csv.gif"));
  loadToExcel.addActionListener(this);
  // For layout purposes, put the buttons in a separate panel
  JPanel buttonPanel = new JPanel(); // use FlowLayout
  buttonPanel.add(openButton);
  buttonPanel.add(textField);
  buttonPanel.add(checkButton);
  Clear = new JButton("Clear", createImageIcon("images/ee.gif"));
  Clear.addActionListener(this);
  JPanel buttonPanel2 = new JPanel();
  buttonPanel2.add(loadToExcel);
  buttonPanel2.add(Clear);
  // Add the buttons and the log to this panel.
  add(buttonPanel, BorderLayout.PAGE_START);
  add(logScrollPane, BorderLayout.CENTER);
  add(buttonPanel2, BorderLayout.PAGE_END);
  checkButton.setEnabled(false);
  loadToExcel.setEnabled(false);
  textField.disable();
 }
 @SuppressWarnings("deprecation")
 public void actionPerformed(ActionEvent e) {
  // Handle open button action.
  if (e.getSource() == openButton) {
   int returnVal = fc.showOpenDialog(FileChooser.this);
   if (returnVal == JFileChooser.APPROVE_OPTION) {
    file = fc.getSelectedFile();
    // This is where a real application would open the file.
    log.append("Opening: " + file + "." + newline);
   } else {
    log.append("Open command cancelled by user." + newline);
   }
   log.setCaretPosition(log.getDocument().getLength());
   checkButton.setEnabled(true);
   textField.enable();
   textField
     .setText("Enter the word(s)/symbol(s) you want to find here");
   textField.requestFocus();
   textField.selectAll();
   // Handle check button action.
  } else if (e.getSource() == checkButton) {
   try {
    msgstorer = new ArrayList();
    text = textField.getText();
    File folder = new File(file.toString());
    File[] listOfFiles = folder.listFiles();
    String[] fileStore = new String[listOfFiles.length];
    log.append("Searching " + text
      + " in the following file(s) were started:" + newline);
    for (int c = 0; c < listOfFiles.length; c++) {
     log.append(listOfFiles[c].getName() + newline);
     fileStore[c] = file.toString() + '\\'
       + listOfFiles[c].getName();
    }
    for (int i = 0; i < fileStore.length; i++) {
     String filer = fileStore[i];
     File file = new File(filer);
     FileChooser fd = new FileChooser();
     String s = fd.getContents(file);
     String fc = s.toLowerCase();
     if (fc.contains(text.toLowerCase())) {
      foundFlag = true;
      msgstorer.add(listOfFiles[i].getName());
     }
    }
    if (foundFlag) {
     log.append("The following file contains  '" + text + "':\n");
     loadToExcel.setEnabled(true);
    } else {
     log.append("None of the file contains  '" + text
       + "' !!!\n");
     loadToExcel.setEnabled(false);
    }
    for (int g = 0; g < msgstorer.size(); g++)
     log.append(msgstorer.get(g) + newline);
   } catch (Exception e1) {
    // TODO Auto-generated catch block
   }
  } else if (e.getSource() == loadToExcel) {
   try {
    Calendar currentDate = Calendar.getInstance();
    SimpleDateFormat formatter = new SimpleDateFormat(
      "dd-MMM-yyy HH_mm_ss");
    String dateNow = formatter.format(currentDate.getTime());
    String chdir = file.toString() + '\\' + "output";
    File directmkr = new File((chdir));
    directmkr.mkdirs();
    String outputfile = file.toString() + '\\' + "output" + '\\'
      + "result " + dateNow + ".csv";
    File creator = new File(outputfile);
    log.append("Creating" + outputfile + newline);
    log.append("Adding the following Details to output file:"
      + newline);
    FileWriter op = new FileWriter(creator);
    BufferedWriter bop = new BufferedWriter(op);
    bop.write("File Name(s) Containing '" + text + "'");
    bop.newLine();
    for (int g = 0; g < msgstorer.size(); g++) {
     String path = file.toString() + '\\' + msgstorer.get(g);
     bop.write(path + newline);
     log.append(path + newline);
    }
    bop.close();
    log.append(newline + "Check the file: " + outputfile
      + "  for the result" + newline);
   } catch (IOException e1) {
    e1.printStackTrace();
   }
  }
  else if (e.getSource() == Clear) {
   log.setText("");
  }
 }
 /** Returns an ImageIcon, or null if the path was invalid. */
 protected static ImageIcon createImageIcon(String path) {
  java.net.URL imgURL = FileChooser.class.getResource(path);
  if (imgURL != null) {
   return new ImageIcon(imgURL);
  } else {
   System.err.println("Couldn't find file: " + path);
   return null;
  }
 }
 /**
  * Create the GUI and show it. For thread safety, this method should be
  * invoked from the event dispatch thread.
  */
 private static void createAndShowGUI() {
  // Create and set up the window.
  JFrame frame = new JFrame("Word Checker");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  // Add content to the window.
  frame.add(new FileChooser());
  // Display the window.
  frame.pack();
  frame.setVisible(true);
 }
 public String getContents(File aFile) {
  StringBuilder contents = new StringBuilder();
  try {
   BufferedReader input = new BufferedReader(new FileReader(aFile));
   try {
    String line = null; // not declared within while loop
    while ((line = input.readLine()) != null) {
     contents.append(line);
     contents.append(System.getProperty("line.separator"));
    }
   } finally {
    input.close();
   }
  } catch (IOException ex) {
   // message
  }
  return contents.toString();
 }
}