1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | 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<string> 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<string>(); 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(); } } </string></string> |