[BAEL-19088] - Move articles out of core-java-io part 3
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.listfiles;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.FileVisitResult;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class ListFiles {
|
||||
public static final int DEPTH = 1;
|
||||
|
||||
public Set<String> listFilesUsingJavaIO(String dir) {
|
||||
return Stream.of(new File(dir).listFiles())
|
||||
.filter(file -> !file.isDirectory())
|
||||
.map(File::getName)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public Set<String> listFilesUsingFileWalk(String dir, int depth) throws IOException {
|
||||
try (Stream<Path> stream = Files.walk(Paths.get(dir), depth)) {
|
||||
return stream.filter(file -> !Files.isDirectory(file))
|
||||
.map(Path::getFileName)
|
||||
.map(Path::toString)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
|
||||
public Set<String> listFilesUsingFileWalkAndVisitor(String dir) throws IOException {
|
||||
Set<String> fileList = new HashSet<>();
|
||||
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
if (!Files.isDirectory(file)) {
|
||||
fileList.add(file.getFileName()
|
||||
.toString());
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
return fileList;
|
||||
}
|
||||
|
||||
public Set<String> listFilesUsingDirectoryStream(String dir) throws IOException {
|
||||
Set<String> fileList = new HashSet<>();
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir))) {
|
||||
for (Path path : stream) {
|
||||
if (!Files.isDirectory(path)) {
|
||||
fileList.add(path.getFileName()
|
||||
.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
return fileList;
|
||||
}
|
||||
|
||||
}
|
||||
-96
@@ -1,96 +0,0 @@
|
||||
package com.baeldung.scanner;
|
||||
|
||||
import lombok.extern.log4j.Log4j;
|
||||
import org.apache.log4j.LogManager;
|
||||
import org.apache.log4j.PropertyConfigurator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
import java.util.Scanner;
|
||||
|
||||
@Log4j
|
||||
public class HasNextVsHasNextLineDemo {
|
||||
private static final String LINE = "----------------------------";
|
||||
private static final String END_LINE = "--------OUTPUT--END---------\n";
|
||||
|
||||
|
||||
private static final String INPUT = new StringBuilder()
|
||||
.append("magic\tproject\n")
|
||||
.append(" database: oracle\n")
|
||||
.append("dependencies:\n")
|
||||
.append("spring:foo:bar\n")
|
||||
.append("\n").toString();
|
||||
|
||||
private static void hasNextBasic() {
|
||||
printHeader("hasNext() Basic");
|
||||
Scanner scanner = new Scanner(INPUT);
|
||||
while (scanner.hasNext()) {
|
||||
log.info(scanner.next());
|
||||
}
|
||||
log.info(END_LINE);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
private static void hasNextWithDelimiter() {
|
||||
printHeader("hasNext() with delimiter");
|
||||
Scanner scanner = new Scanner(INPUT);
|
||||
while (scanner.hasNext()) {
|
||||
String token = scanner.next();
|
||||
if ("dependencies:".equals(token)) {
|
||||
scanner.useDelimiter(":");
|
||||
}
|
||||
log.info(token);
|
||||
}
|
||||
log.info(END_LINE);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
private static void hasNextWithDelimiterFixed() {
|
||||
printHeader("hasNext() with delimiter FIX");
|
||||
Scanner scanner = new Scanner(INPUT);
|
||||
while (scanner.hasNext()) {
|
||||
String token = scanner.next();
|
||||
if ("dependencies:".equals(token)) {
|
||||
scanner.useDelimiter(":|\\s+");
|
||||
}
|
||||
log.info(token);
|
||||
}
|
||||
log.info(END_LINE);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
private static void addLineNumber() {
|
||||
printHeader("add line number by hasNextLine() ");
|
||||
Scanner scanner = new Scanner(INPUT);
|
||||
int i = 0;
|
||||
while (scanner.hasNextLine()) {
|
||||
log.info(String.format("%d|%s", ++i, scanner.nextLine()));
|
||||
}
|
||||
log.info(END_LINE);
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
private static void printHeader(String title) {
|
||||
log.info(LINE);
|
||||
log.info(title);
|
||||
log.info(LINE);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
setLogger();
|
||||
hasNextBasic();
|
||||
hasNextWithDelimiter();
|
||||
hasNextWithDelimiterFixed();
|
||||
addLineNumber();
|
||||
}
|
||||
|
||||
//overwrite the logger config
|
||||
private static void setLogger() throws IOException {
|
||||
InputStream is = HasNextVsHasNextLineDemo.class.getResourceAsStream("/scanner/log4j.properties");
|
||||
Properties props = new Properties();
|
||||
props.load(is);
|
||||
LogManager.resetConfiguration();
|
||||
PropertyConfigurator.configure(props);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
log4j.rootLogger=INFO, A1
|
||||
log4j.appender.A1=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.A1.layout.ConversionPattern=[DEMO]%m%n
|
||||
Reference in New Issue
Block a user