q7goodies事例_Java 8 Friday Goodies:java.io终于成功了!
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);}};}@FunctionalInterfaceprivate 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 nulldir.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事例
總結
以上是生活随笔為你收集整理的q7goodies事例_Java 8 Friday Goodies:java.io终于成功了!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微店设置分销(微店怎么取消全店分销模式)
- 下一篇: 对电脑整个硬盘备份(系统备份到硬盘)