diff --git a/src/main/java/hello/controller/HelloController.java b/src/main/java/hello/controller/HelloController.java index f3602fa..0c30e60 100644 --- a/src/main/java/hello/controller/HelloController.java +++ b/src/main/java/hello/controller/HelloController.java @@ -29,10 +29,6 @@ public class HelloController { "Select ID With \"Java\" Keyword," + " Then Sort Then Join "; String findIdHavingCharacterTemplate = "-------------Return All ID having character \'g\' in it: "; - String findAllFilesInPathAndSortTemplate = "---------Find all files in path and sort: "; - String findParticularFileInPathAndSortTemplate = "----------Find File in present directory which strats with \"grad\",provided maximum depth=25 and sort : "; - String findParticularFileInPathAndSortWithWalkFunctionTemplate = "----------Find File in present directory which strats with \"grad\",provided maximum depth=25 and sort : with walk function"; - String readFileWithStreamFunctionTemplate = "---------Read \"temp.txt\" file with stream functions, having \"print\" witin it: "; @Autowired @@ -80,22 +76,4 @@ public String showStringOperation() { } - /** - * File Operation in Java 8 - * @return - */ - @RequestMapping("/topic/file/operation") - public String showFileOperation() { - String findAllFilesInPathAndSort = topicService.findAllFilesInPathAndSort(); - String findParticularFileInPathAndSort = topicService.findParticularFileInPathAndSort(); - String findParticularFileInPathAndSortWithWalkFunction = topicService.findParticularFileInPathAndSortWithWalkFunction(); - String readFileWithStreamFunction = topicService.readFileWithStreamFunction(); - return findAllFilesInPathAndSortTemplate + findAllFilesInPathAndSort - + findParticularFileInPathAndSortTemplate + findParticularFileInPathAndSort - + findParticularFileInPathAndSortWithWalkFunctionTemplate + findParticularFileInPathAndSortWithWalkFunction - + readFileWithStreamFunctionTemplate + readFileWithStreamFunction; - } - - - } diff --git a/src/main/java/hello/service/TopicService.java b/src/main/java/hello/service/TopicService.java index feffb92..c447a13 100644 --- a/src/main/java/hello/service/TopicService.java +++ b/src/main/java/hello/service/TopicService.java @@ -4,11 +4,6 @@ import hello.model.Topic; import org.springframework.stereotype.Service; -import java.io.BufferedReader; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.*; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -157,84 +152,4 @@ public String findIdHavingCharacter() { .toString(); } - - /** - * NIO Java API - * Use Streams with files - * Try with resource, "Autoclose" - * - * @return - */ - public String findAllFilesInPathAndSort() { - try (Stream stream = Files.list(Paths.get(""))) { - String joined = stream - .map(String::valueOf) - .filter(path -> !path.startsWith(".")) - .sorted() - .collect(Collectors.joining("; ")); - return joined; - } catch (IOException e) { - return " Error in IO"; - } - } - - /** - * Using File.find function to find file - * @return - */ - public String findParticularFileInPathAndSort() { - Path start = Paths.get(""); - int maxDepth = 25; - try (Stream stream = Files.find(start, maxDepth, (path, attr) -> - String.valueOf(path).startsWith("grad"))) { - String joined = stream - .sorted() - .map(String::valueOf) - .collect(Collectors.joining("; ")); - return joined; - } catch (IOException e) { - return " IO exception "; - } - } - - - /** - * Using Files.Walk Function to find File - * @return - */ - public String findParticularFileInPathAndSortWithWalkFunction() { - Path start = Paths.get(""); - int maxDepth = 5; - try (Stream stream = Files.walk(start, maxDepth)) { - String joined = stream - .map(String::valueOf) - .filter(path -> path.startsWith("grad")) - .sorted() - .collect(Collectors.joining("; ")); - return joined; - } catch (IOException e) { - return " IO exception "; - } - } - - - /** - * Use BufferedReader with Stream functions - * @return - */ - public String readFileWithStreamFunction() { - Path path = Paths.get("temp.txt"); - System.out.println(); - try (BufferedReader reader = Files.newBufferedReader(path)) { - String lines = reader - .lines() - .filter(line->line.contains("print")) - .map(line->line.substring("print".length())) - .collect(Collectors.joining(",")); - return lines; - } catch (IOException e) { - return " IO exception "; - } - } - }