Java 8 Friday Goodies:java.io终于成功了!
在Data Geekery ,我們喜歡Java。 而且,由于我們真的很喜歡jOOQ的流暢的API和查詢(xún)DSL ,我們對(duì)Java 8將為我們的生態(tài)系統(tǒng)帶來(lái)什么感到非常興奮。 我們已經(jīng)寫(xiě)了一些關(guān)于Java 8好東西的博客 ,現(xiàn)在我們覺(jué)得是時(shí)候開(kāi)始一個(gè)新的博客系列了……
Java 8星期五
每個(gè)星期五,我們都會(huì)向您展示一些不錯(cuò)的教程風(fēng)格的Java 8新功能,這些功能利用了lambda表達(dá)式,擴(kuò)展方法和其他好東西。 您可以在GitHub上找到源代碼 。
Java 8 Goodie:帶有Lambdas的java.io
與文件系統(tǒng)進(jìn)行交互在Java中有些痛苦。 CTMMC向我們展示了如何使用Java復(fù)制文件的示例 。 盡管仍然存在一些問(wèn)題,至少,我們現(xiàn)在可以使用lambda和新的Streams API 遍歷文件系統(tǒng)并列出文件 ! 這是我們已推送到GitHub存儲(chǔ)庫(kù)的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現(xiàn)在,這真的很棒,不是嗎? 讓我們分解上面的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);}}));下周還會(huì)有更多好吃的東西
請(qǐng)繼續(xù)關(guān)注下周,當(dāng)我們向您展示如何將Java 8 與jOOX一起使用XML進(jìn)行改進(jìn)時(shí)
翻譯自: https://www.javacodegeeks.com/2014/01/java-8-friday-goodies-java-io-finally-rocks.html
總結(jié)
以上是生活随笔為你收集整理的Java 8 Friday Goodies:java.io终于成功了!的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 数学特殊符号怎么输入电脑的特殊符号如何打
- 下一篇: 怀化特产(来怀化旅游,这九大美食千万不要