Description : Following program can be used to search the files(java,jsp and js) for the given list of keywords. If present, the absolute path and Line number of occurrence will be written to the text file.
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 |
package com.example; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.LineNumberReader; import java.util.ArrayList; import java.util.Iterator; public class ContentSearching { public static int count = 0; public ArrayList<String> readListOfItemsToBeSearched() { ArrayList<String> itemsList = new ArrayList<String>(); BufferedReader br = null; // Path of file containing the list of keywords to be searched File file = new File("D:\\ProjectCodeBank\\resources\\list.txt"); if (file.isFile()) { try { String str = null; br = new BufferedReader(new FileReader(file)); while ((str = br.readLine()) != null) { itemsList.add(str); } br.close(); } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } else { System.out.println("Please specify the path for text file"); System.exit(0); } // returning list of keywords (to be searched) as arraylist return itemsList; } public void listingFiles(String path, String item) { File file = new File(path); File[] fileArr = file.listFiles(); for (File f : fileArr) { if (f.isDirectory()) { listingFiles(f.getAbsolutePath(), item); } else if (f.isFile()) { String fileName = f.getName(); if (fileName.contains(".")) { // split() function doesn't worl for periods(.) fileName = fileName.replace(".", "~"); String extension = fileName.split("~")[1]; /* We only want to search java, jsp and js files */ if (extension.equals("java") || extension.equals("jsp") || extension.equals("js")) { searchContent(f, item); } else { continue; } } else { continue; } } } } public void searchContent(File searchFile, String forItem) { String searchResult = "result_" + count + ".txt"; // Location of folder where our search results will be stored File file = new File("D:\\ProjectCodeBank\\resources\\searchResult", searchResult); BufferedWriter bw = null; LineNumberReader br = null; try { long fileSize = file.length(); // if the size of file is exceeding 1MB then we will create new file if (fileSize > 1048576) { count++; searchResult = "result_" + count + ".txt"; file = new File("D:\\ProjectCodeBank\\resources\\searchResult", searchResult); } bw = new BufferedWriter(new FileWriter(file, true)); br = new LineNumberReader(new FileReader(searchFile)); String str = "**********" + forItem + "****************"; // bw.write(str); // bw.newLine(); boolean flag = false; while ((str = br.readLine()) != null) { if (str.contains(forItem)) { if (flag == false) { bw.newLine(); // Path of the file where the keyword is found bw.write(searchFile.getPath() + ":"); bw.newLine(); flag = true; } // Line Number where the keyword id found bw.write(br.getLineNumber() + ":" + str); bw.newLine(); } } br.close(); bw.close(); } catch (SecurityException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } public static void main(String args[]) { ContentSearching cs = new ContentSearching(); ArrayList<String> arrList = cs.readListOfItemsToBeSearched(); Iterator<String> it = arrList.iterator(); while (it.hasNext()) { String item = it.next(); System.out.println(item); String searchResult = "result_" + count + ".txt"; // Path of file containing the search result File file = new File( "D:\\ProjectCodeBank\\resources\\searchResult", searchResult); try { BufferedWriter bw = new BufferedWriter(new FileWriter(file, true)); bw.write("**********************" + item + "*****************************"); bw.newLine(); bw.flush(); bw.close(); } catch (IOException ioe) { ioe.printStackTrace(); } // Folder to be searched and the item name to be searched for cs.listingFiles("D:\\ProjectCodeBank", item); } } } |