Advertisement
nazar_art

SearchPhrase - 02 variant

Jan 23rd, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package task;
  2.  
  3. import java.io.*;
  4.  
  5. class SearchPhrase {
  6.  
  7.     // walk to root way
  8.     public void walk(String path, String whatFind) throws IOException {
  9.  
  10.         File root = new File(path);
  11.         File[] list = root.listFiles();
  12.         for (File titleName : list) {
  13.             if (titleName.isDirectory()) {
  14.                 walk(titleName.getAbsolutePath(), whatFind);
  15.             } else {
  16.                 if (read(titleName.getAbsolutePath()).contains(whatFind)) {
  17.                     System.out.println("File: " + titleName.getAbsoluteFile());
  18.                 }
  19.             }
  20.         }
  21.     }
  22.  
  23.     // Read file as one line
  24.     public static String read(String fileName) {
  25.         StringBuilder strBuider = new StringBuilder();
  26.         try {
  27.             BufferedReader in = new BufferedReader(new FileReader(new File(
  28.                     fileName)));
  29.             String strInput;
  30.             while ((strInput = in.readLine()) != null) {
  31.                 strBuider.append(strInput);
  32.                 strBuider.append("\n");
  33.             }
  34.  
  35.             in.close();
  36.         } catch (IOException e) {
  37.             e.printStackTrace();
  38.         }
  39.  
  40.         return strBuider.toString();
  41.     }
  42.  
  43.     public static void main(String[] args) {
  44.  
  45.         SearchPhrase example = new SearchPhrase();
  46.  
  47.         try {
  48.             example.walk(
  49.                     "C:\\Documents and Settings\\User\\Java Hangman\\Java\\Anton",
  50.                     "programmed");
  51.         } catch (IOException e) {
  52.             e.printStackTrace();
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement