Advertisement
nazar_art

SearchPhrase - 04 variant all work

Jan 24th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  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.getAbsolutePath());
  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.         example.askUserPathAndWord();
  47.     }
  48.    
  49.     public void askUserPathAndWord() {
  50.  
  51.         BufferedReader bufferedReader = new BufferedReader(
  52.                 new InputStreamReader(System.in));
  53.         String path, whatFind;
  54.         try {
  55.             System.out.println("Please, enter a Path and Word"
  56.                     + "(which you want to find):");
  57.             System.out.println("Please enter a Path:");
  58.             path = bufferedReader.readLine();
  59.             System.out.println("Please enter a Word:");
  60.             whatFind = bufferedReader.readLine();
  61.  
  62.             if (path != null && whatFind != null) {
  63.                 walk(path, whatFind);
  64.                 System.out.println("Thank you!");
  65.             } else {
  66.                 System.out.println("You did not enter anything");
  67.             }
  68.  
  69.         } catch (IOException | RuntimeException e) {
  70.             System.out.println("Wrong input!");
  71.             e.printStackTrace();
  72.         }
  73.     }
  74. }
  75. ------------------------------------------------------------------
  76. Runtime of the program:
  77.  
  78. package task;
  79. import java.io.IOException;
  80.  
  81. public class SearchPhrase_TimeTest1 {
  82.  
  83.     private SearchPhrase example = new SearchPhrase();
  84.  
  85.     public void main(String[] args) {
  86.         long startTime = System.currentTimeMillis();
  87.         try {example.walk("E:\\Document\\!Nazar\\АНГЛІЙСЬКА\\"
  88.              + "Effortless English\\Effortless English-New method learning
  89.              english\\Level 3","spot");
  90.         } catch (IOException e) {
  91.             e.printStackTrace();
  92.         }
  93.         long stopTime = System.currentTimeMillis();
  94.         long elapsedTime = stopTime - startTime;
  95.         System.out.println(elapsedTime);
  96.     }
  97. }
  98. -----------------------------------------------------------------
  99. Memory Consumption:
  100.  
  101. package task;
  102. public class PerformanceTest {
  103.  
  104.      private static final long MEGABYTE = 1024L * 1024L;
  105.  
  106.       public static long bytesToMegabytes(long bytes) {
  107.         return bytes / MEGABYTE;
  108.       }
  109.  
  110.       public static void main(String[] args) {
  111.          
  112.         SearchPhrase example = new SearchPhrase();
  113.         example.askUserPathAndWord();      
  114.          
  115.         // Get the Java runtime
  116.         Runtime runtime = Runtime.getRuntime();
  117.         // Run the garbage collector
  118.         runtime.gc();
  119.         // Calculate the used memory
  120.         long memory = runtime.totalMemory() - runtime.freeMemory();
  121.         System.out.println("Used memory is bytes: " + memory);
  122.         System.out.println("Used memory is megabytes: "
  123.             + bytesToMegabytes(memory));
  124.       }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement