Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package task;
- import java.io.*;
- class SearchPhrase {
- // walk to root way
- public void walk(String path, String whatFind) throws IOException {
- File root = new File(path);
- File[] list = root.listFiles();
- for (File titleName : list) {
- if (titleName.isDirectory()) {
- walk(titleName.getAbsolutePath(), whatFind);
- } else {
- if (read(titleName.getAbsolutePath()).contains(whatFind)) {
- System.out.println("File:" + titleName.getAbsolutePath());
- }
- }
- }
- }
- // Read file as one line
- public static String read(String fileName) {
- StringBuilder strBuider = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new FileReader(new File(
- fileName)));
- String strInput;
- while ((strInput = in.readLine()) != null) {
- strBuider.append(strInput);
- strBuider.append("\n");
- }
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return strBuider.toString();
- }
- public static void main(String[] args) {
- SearchPhrase example = new SearchPhrase();
- example.askUserPathAndWord();
- }
- public void askUserPathAndWord() {
- BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(System.in));
- String path, whatFind;
- try {
- System.out.println("Please, enter a Path and Word"
- + "(which you want to find):");
- System.out.println("Please enter a Path:");
- path = bufferedReader.readLine();
- System.out.println("Please enter a Word:");
- whatFind = bufferedReader.readLine();
- if (path != null && whatFind != null) {
- walk(path, whatFind);
- System.out.println("Thank you!");
- } else {
- System.out.println("You did not enter anything");
- }
- } catch (IOException | RuntimeException e) {
- System.out.println("Wrong input!");
- e.printStackTrace();
- }
- }
- }
- ------------------------------------------------------------------
- Runtime of the program:
- package task;
- import java.io.IOException;
- public class SearchPhrase_TimeTest1 {
- private SearchPhrase example = new SearchPhrase();
- public void main(String[] args) {
- long startTime = System.currentTimeMillis();
- try {example.walk("E:\\Document\\!Nazar\\АНГЛІЙСЬКА\\"
- + "Effortless English\\Effortless English-New method learning
- english\\Level 3","spot");
- } catch (IOException e) {
- e.printStackTrace();
- }
- long stopTime = System.currentTimeMillis();
- long elapsedTime = stopTime - startTime;
- System.out.println(elapsedTime);
- }
- }
- -----------------------------------------------------------------
- Memory Consumption:
- package task;
- public class PerformanceTest {
- private static final long MEGABYTE = 1024L * 1024L;
- public static long bytesToMegabytes(long bytes) {
- return bytes / MEGABYTE;
- }
- public static void main(String[] args) {
- SearchPhrase example = new SearchPhrase();
- example.askUserPathAndWord();
- // Get the Java runtime
- Runtime runtime = Runtime.getRuntime();
- // Run the garbage collector
- runtime.gc();
- // Calculate the used memory
- long memory = runtime.totalMemory() - runtime.freeMemory();
- System.out.println("Used memory is bytes: " + memory);
- System.out.println("Used memory is megabytes: "
- + bytesToMegabytes(memory));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement