Required jar files: 1. commons-io-1.3.2.jar 2. l2fprod-common-all.jar Inputs for Application while Runnig: 1) * Source Directory 2) * Destination Directory 3) * File Extension 4) Search String ( Optional ) Source Code 1.Java_File_SearchEngine.java
|
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 |
package com.search;
import com.l2fprod.common.swing.JDirectoryChooser;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import javax.swing.*;
import javax.swing.JOptionPane;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
public class Java_File_SearchEngine extends JFrame implements ActionListener {
Container container;
JLabel labelSourceDirectory;
JLabel labelDestinationDirectory;
JLabel labelFileExtension;
JLabel labelSearchString;
JLabel labelOptional;
JTextField textFieldSourceDirectory;
JTextField textFieldDestinationDirectory;
JTextField textFieldFileExtension;
JTextField textFieldSearchString;
JButton buttonChooseSourceDirectory;
JButton buttonChooseDestinationDirectory;
JButton buttonSearchFiles;
public Java_File_SearchEngine() {
container = getContentPane();
container.setLayout(null);
labelSourceDirectory = new JLabel("* Source Directory :");
labelDestinationDirectory = new JLabel("* Destination Directory :");
labelFileExtension = new JLabel("* File Extension :");
labelSearchString = new JLabel("Search String :");
labelOptional = new JLabel("( Optional )");
textFieldSourceDirectory = new JTextField();
textFieldDestinationDirectory = new JTextField();
textFieldFileExtension = new JTextField();
textFieldSearchString = new JTextField();
buttonChooseSourceDirectory = new JButton("Choose Source Directory");
buttonChooseDestinationDirectory = new JButton("Choose Destination Directory");
buttonSearchFiles = new JButton("Search Files");
labelSourceDirectory.setBounds(10, 10, 200, 30);
textFieldSourceDirectory.setBounds(150, 10, 200, 30);
buttonChooseSourceDirectory.setBounds(370, 10, 200, 30);
labelDestinationDirectory.setBounds(10, 50, 200, 30);
textFieldDestinationDirectory.setBounds(150, 50, 200, 30);
buttonChooseDestinationDirectory.setBounds(370, 50, 200, 30);
labelFileExtension.setBounds(10, 100, 200, 30);
textFieldFileExtension.setBounds(150, 100, 200, 30);
labelSearchString.setBounds(10, 150, 200, 30);
textFieldSearchString.setBounds(150, 150, 200, 30);
labelOptional.setBounds(370, 150, 200, 30);
buttonSearchFiles.setBounds(150, 200, 200, 30);
container.add(labelSourceDirectory);
container.add(textFieldSourceDirectory);
container.add(buttonChooseSourceDirectory);
container.add(labelDestinationDirectory);
container.add(textFieldDestinationDirectory);
container.add(buttonChooseDestinationDirectory);
container.add(labelFileExtension);
container.add(textFieldFileExtension);
container.add(labelSearchString);
container.add(textFieldSearchString);
container.add(labelOptional);
container.add(buttonSearchFiles);
buttonChooseSourceDirectory.addActionListener(this);
buttonChooseDestinationDirectory.addActionListener(this);
buttonSearchFiles.addActionListener(this);
setTitle("Nandeeswar Search Engine");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws IOException {
Java_File_SearchEngine frame = new Java_File_SearchEngine();
frame.setSize(600, 300);
frame.setVisible(true);
}//end of main()
public static boolean searchStringInFile(File inputFile, String inputString) {
try {
String stringSearch = inputString;
BufferedReader bf = new BufferedReader(new FileReader(inputFile.getPath()));
int linecount = 0;
String line;
System.out.println("Searching for String \" " + stringSearch + " \" in file...");
while ((line = bf.readLine()) != null) {
linecount++;
int indexfound = line.indexOf(stringSearch);
if (indexfound > -1) {
System.out.println("Word was found at position " + indexfound + " on line " + linecount);
return true;
}
}
bf.close();
} catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());
return false;
}
return false;
}
public static String getDirectory() {
String directoryPath = "";
JFrame frame = new JFrame();
frame.setLayout(new BorderLayout());
JDirectoryChooser chooser = new JDirectoryChooser();
int choice = chooser.showOpenDialog(frame);
if (choice == JDirectoryChooser.CANCEL_OPTION) {
System.out.println("User Canceled");
} else {
directoryPath = chooser.getSelectedFile().getAbsolutePath();
System.out.println("Dialog Selection: " + directoryPath);
}
return directoryPath;
}
@Override
public void actionPerformed(ActionEvent event) {
Object button = event.getSource();
if (button == buttonChooseSourceDirectory) {
textFieldSourceDirectory.setText(Java_File_SearchEngine.getDirectory());
} else if (button == buttonChooseDestinationDirectory) {
textFieldDestinationDirectory.setText(Java_File_SearchEngine.getDirectory());
} else if (button == buttonSearchFiles) {
try {
Thread.sleep(2000);
} catch (Exception e) {
}
//Search File's
try {
File sourceDirectory = new File(textFieldSourceDirectory.getText().replace("\\", "/"));
String destinationDirectory = textFieldDestinationDirectory.getText().replace("\\", "/") + "/";
String fileExtension = textFieldFileExtension.getText();// kind of files to copy into destination directory
String searchString = textFieldSearchString.getText();
boolean fileFound = false;
System.out.println("Source: " + textFieldSourceDirectory.getText().replace("\\", "/"));
System.out.println("destinationDirectory: " + destinationDirectory);
System.out.println("fileExtension: " + fileExtension);
System.out.println("searchString: " + searchString);
System.out.println("Getting all files in " + sourceDirectory.getCanonicalPath() + " including those in subdirectories");
System.out.println("Loading...");
List<File> files = (List<File>) FileUtils.listFiles(sourceDirectory, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE);
int fileNo = 1;
int foundFilesNo = 0;
for (File file : files) {
//Getting FilePath,FileName,FileExtension
String filePath = "", fileName = "", ext = "";
filePath = file.getPath();
int lastSlash = filePath.lastIndexOf("\\");
int lastDot = filePath.lastIndexOf(".");
fileName = filePath.substring(lastSlash + 1, filePath.length());
ext = filePath.substring(lastDot + 1, filePath.length());
// Copying search string contained text files into speified destination directory...
if (ext.equalsIgnoreCase(fileExtension)) {
System.out.println((fileNo++) + ".File name :" + fileName + " (Path: " + filePath + ")");
System.out.println("Extension :" + ext);
if (textFieldSearchString.getText().equalsIgnoreCase("")) {
fileFound = true;
File copyFile = new File(destinationDirectory + fileName);
copyFile.createNewFile();
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(copyFile, true); // appending output stream
foundFilesNo++;
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
} else {
boolean searchResult = Java_File_SearchEngine.searchStringInFile(file, searchString);
Thread.sleep(2000);
System.out.println("searchResult: " + searchResult);
if (searchResult) {
fileFound = true;
foundFilesNo++;
File copyFile = new File(destinationDirectory + fileName);
copyFile.createNewFile();
InputStream in = new FileInputStream(file);
OutputStream out = new FileOutputStream(copyFile, true); // appending output stream
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
}
}
}
if (fileFound) {
if (foundFilesNo == 1) {
//JOptionPane.showMessageDialog(null, "Found : " + foundFilesNo + " file");
} else if (foundFilesNo > 1) {
// JOptionPane.showMessageDialog(null, "Found : " + foundFilesNo / 2 + " files");
}
Desktop.getDesktop().open(new File(destinationDirectory));
} else {
JOptionPane.showMessageDialog(null, "No Such File's Found with search String: " + searchString);
}
} catch (Exception e) {
}
}
}
}
//end of code... |
What do you think of this post?Awesome (1) Interesting (0) Useful (0) Boring (0) Sucks (0)