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);
            }
        };
    }

    @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时

参考: Java 8 Friday Goodies:java.io终于成功了! 从我们的JCG合作伙伴 Lukas Eder在JAVA,SQL和JOOQ博客中获得。

翻译自: https://www.javacodegeeks.com/2014/01/java-8-friday-goodies-java-io-finally-rocks.html

q7goodies事例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值