Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 0 additions & 22 deletions src/main/java/hello/controller/HelloController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}



}
85 changes: 0 additions & 85 deletions src/main/java/hello/service/TopicService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Path> 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<Path> 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<Path> 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 ";
}
}

}