Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
Java8Musings/src/main/java/net/briandupreez/blog/java8/io/RecursiveFileLineReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package net.briandupreez.blog.java8.io; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.file.FileVisitOption; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
/** | ||
* RecursiveFileLineReader | ||
* Created by Brian on 2014-05-26. | ||
*/ | ||
public class RecursiveFileLineReader { | ||
|
||
private transient static final Log LOG = LogFactory.getLog(RecursiveFileLineReader.class); | ||
|
||
/** | ||
* Get all the non empty lines from all the files with the specific extension, recursively. | ||
* | ||
* @param path the path to start recursion | ||
* @param extension the file extension | ||
* @return list of lines | ||
*/ | ||
public static List<String> readAllLineFromAllFilesRecursively(final String path, final String extension) { | ||
final List<String> lines = new ArrayList<>(); | ||
try (final Stream<Path> pathStream = Files.walk(Paths.get(path), FileVisitOption.FOLLOW_LINKS)) { | ||
pathStream | ||
.filter((p) -> !p.toFile().isDirectory() && p.toFile().getAbsolutePath().endsWith(extension)) | ||
.forEach(p -> fileLinesToList(p, lines)); | ||
} catch (final IOException e) { | ||
LOG.error(e.getMessage(), e); | ||
} | ||
return lines; | ||
} | ||
|
||
private static void fileLinesToList(final Path file, final List<String> lines) { | ||
try (Stream<String> stream = Files.lines(file, Charset.defaultCharset())) { | ||
stream | ||
.map(String::trim) | ||
.filter(s -> !s.isEmpty()) | ||
.forEach(lines::add); | ||
} catch (final IOException e) { | ||
LOG.error(e.getMessage(), e); | ||
} | ||
} | ||
|
||
|
||
} |
16 changes: 16 additions & 0 deletions
16
Java8Musings/src/test/java/net/briandupreez/blog/java8/io/RecursiveFileLineReaderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package net.briandupreez.blog.java8.io; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class RecursiveFileLineReaderTest { | ||
|
||
@Test | ||
public void testReadAllLineFromAllFilesRecursively() throws Exception { | ||
final List<String> strings = RecursiveFileLineReader.readAllLineFromAllFilesRecursively(".", ".java"); | ||
assertFalse(strings.isEmpty()); | ||
} | ||
} |