q7goodies事例
在Data Geekery ,我们喜欢Java。 而且,由于我们真的很喜欢jOOQ的流畅的API和查询DSL ,我们对Java 8将为我们的生态系统带来什么感到非常兴奋。 我们已经写了一些关于Java 8好东西的博客 ,现在我们觉得是时候开始一个新的博客系列了,……
Java 8星期五
每个星期五,我们都会向您展示一些不错的教程风格的Java 8新功能,这些功能利用了lambda表达式,扩展方法和其他出色的功能。 您可以在GitHub上找到源代码 。
Java 8 Goodie:带有Lambdas的java.io
与文件系统进行交互在Java中有些痛苦。 CTMMC向我们展示了如何使用Java复制文件的示例 。 尽管仍然存在一些问题,至少,我们现在可以使用lambda和新的Streams API 遍历文件系统并列出文件 ! 这是我们已推送到GitHub存储库的FileFilterGoodies示例:
public class FileFilterGoodies {
public static void main(String args[]) {
listRecursive(new File("."));
}
/**
* This method recursively lists all
* .txt and .java files in a directory
*/
private static void listRecursive(File dir) {
Arrays.stream(dir.listFiles((f, n) ->
!n.startsWith(".")
&&
(f.isDirectory()
|| n.endsWith(".txt")
|| n.endsWith(".java"))
))
.forEach(unchecked((file) -> {
System.out.println(
file.getCanonicalPath()
.substring(new File(".")
.getCanonicalPath()
.length()));
if (file.isDirectory()) {
listRecursive(file);
}
}));
}
/**
* This utility simply wraps a functional
* interface that throws a checked exception
* into a Java 8 Consumer
*/
private static <T> Consumer<T>
unchecked(CheckedConsumer<T> consumer) {
return t -> {
try {
consumer.accept(t);
}
catch (Exception e) {
throw new RuntimeException(e);
}
};
}
@FunctionalInterface
private interface CheckedConsumer<T> {
void accept(T t) throws Exception;
}
}
上面程序的输出是:
\jOOQ's Java 8 Goodies.iml
\LICENSE.txt
\out
\out\production
\out\production\jOOQ's Java 8 Goodies
\out\production\jOOQ's Java 8 Goodies\org
\out\production\jOOQ's Java 8 Goodies\org\jooq
\out\production\jOOQ's Java 8 Goodies\org\jooq\java8
\out\production\jOOQ's Java 8 Goodies\org\jooq\java8\goodies
\out\production\jOOQ's Java 8 Goodies\org\jooq\java8\goodies\io
\out\production\jOOQ's Java 8 Goodies\org\jooq\java8\goodies\io\FileFilterGoodies$CheckedConsumer.class
\out\production\jOOQ's Java 8 Goodies\org\jooq\java8\goodies\io\FileFilterGoodies.class
\README.txt
\src
\src\org
\src\org\jooq
\src\org\jooq\java8
\src\org\jooq\java8\goodies
\src\org\jooq\java8\goodies\io
\src\org\jooq\java8\goodies\io\FileFilterGoodies.java
现在,这真的很棒,不是吗? 让我们分解上面的listRecursive()
方法:
// With this method, we wrap the File[] array
// into a new Java 8 Stream, which has awesome
// new methods.
Arrays.stream(
// The Java 1.2 File.listFiles() method luckily
// accepts a @FunctionalInterface, which can be
// instantiated using a lambda expression
// ...
// In this example, we'll just ignore the fact
// that listFiles can return null
dir.listFiles((f, n) ->
!n.startsWith(".")
&&
(f.isDirectory()
|| n.endsWith(".txt")
|| n.endsWith(".java"))
))
// Each Stream (and also java.util.List) has this
// awesome forEach method, that accepts a Consumer
.forEach(
// Unfortunately, Java 8 Consumers don't allow
// throwing checked exceptions. So let's quickly
// wrap it (see details below) ...
unchecked(
// ... and pass another lambda expression to it,
// which prints the local path and recurses
(file) -> {
System.out.println(
file.getCanonicalPath()
.substring(new File(".")
.getCanonicalPath()
.length()));
if (file.isDirectory()) {
listRecursive(file);
}
}));
下周会有更多好吃的东西
请继续关注下周,当我们向您展示如何在jOOX中使用XML改进Java 8时
翻译自: https://www.javacodegeeks.com/2014/01/java-8-friday-goodies-java-io-finally-rocks.html
q7goodies事例