如何在 Scala 中使用 java.nio.file.Files.walkFileTree
我想在 Scala 中使用新的 java.nio.file.Files.walkFileTree
。我什至成功了:
class Visitor
extends
java.nio.file.SimpleFileVisitor [java.nio.file.Path]
{
override def visitFile(
File : java.nio.file.Path,
Attrs : java.nio.file.attribute.BasicFileAttributes) : java.nio.file.FileVisitResult =
{
if (! File.toString.contains(".svn"))
{
System.out.println(File);
} // if
java.nio.file.FileVisitResult.CONTINUE;
} // visitFile
} // Visitor
java.nio.file.Files.walkFileTree (Project_Home, new Visitor)
虽然这段代码运行良好,但我感觉有点像将 Java 范例引入 Scala。因此,向真正的 Scala 大师提出一个问题:有什么我可以改进的地方,还是仅此而已?
I want to use the new java.nio.file.Files.walkFileTree
in Scala. And I was even successful:
class Visitor
extends
java.nio.file.SimpleFileVisitor [java.nio.file.Path]
{
override def visitFile(
File : java.nio.file.Path,
Attrs : java.nio.file.attribute.BasicFileAttributes) : java.nio.file.FileVisitResult =
{
if (! File.toString.contains(".svn"))
{
System.out.println(File);
} // if
java.nio.file.FileVisitResult.CONTINUE;
} // visitFile
} // Visitor
java.nio.file.Files.walkFileTree (Project_Home, new Visitor)
But while this code works fine I feels a bit like carrying Java paradigms into Scala. So a question to the true Scala Gurus: Is there anything I could improve or is this just it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Visitor 实际上是一个没有函数优势的
foreach
,所以让我们创建一个foreach
。该方法是静态的,但它采用Path
作为第一个参数,因此我们将使用foreach
方法丰富Path
,该方法是通过像这样的东西:其他所有内容都在
TraversePath
类中,看起来有点像这样:这足以让你这样写:
当然,它实际上不会做任何事情,所以让我们得到它做某事:
现在那条线将做同样的事情你的代码做到了!然而,我们可以进一步改进它。碰巧
foreach
是Traversable
所需的唯一方法,因此我们可以扩展该类,并获取 Scala 集合的所有方法!唯一的问题是 Traversable.foreach 函数只接受一个参数,而这里我们接受两个参数。不过,我们可以将其更改为接收元组。这是完整的代码:
免责声明:我没有测试过这些代码,因为我没有安装 Java 7。可能存在一些错误。
A Visitor is really a
foreach
without the benefit of functions, so let's make aforeach
. The method is static, but it takes as first argument aPath
, so we'll enrichPath
with aforeach
method, which is done with something like this:And everything else is inside the
TraversePath
class, which looks somewhat like this:This is enough for you to write this:
Of course, it won't actually do anything, so let's get it to do something:
There, now that line will do the same thing as your code did! However, we can improve it further. It happens that
foreach
is the only method required ofTraversable
, so we can extend that class, and get all the methods of a Scala collection!The only problem is that a
Traversable.foreach
function takes only one argument, and here we are taking two. We can change it into receive a tuple, though. Here's the full code:Disclaimer: I have tested none of this code, because I don't have Java 7 installed. There are probably some bugs.
以 Daniel 的回答为基础,我做了一些工作,使
Path
可通过方便的隐式访问,正如您在集合中使用的那样。请注意,并非包含所有功能。java.nio API 非常强大,恕我直言,非常适合与 Scala 一起使用。有了这些隐式(还有更多,如果你想编写一些函数),完成更困难的任务就变得非常简单。
您现在可以通过编写如下内容来使用它:
问候,
丹尼尔
Taking Daniel's answer as fundament, I have worked a little to make
Path
accessible with convenient implicits, as you are used in collections. Notice that not all functions are included.The java.nio API is extremely powerful and is, IMHO, very sufficing for use with Scala. With these implicits (and more, if you want to write some functions), it is very simple to accomplish even harder tasks.
You could use this now by writing something like this:
Regards,
Danyel
这是丹尼尔的可编译脚本:
Here's Daniel's script made compilable:
您可以使代码更漂亮一些,但最终它看起来仍然像普通的老访客模式。
You could make your code a bit more pretty, but at the end of the day it would still look like the plain old visitor pattern.
FIles.walkFileTree 比较两个目录/同步两个目录的示例
对于文件差异
FIles.walkFileTree example to compare two directories / synchronizing two directories
for file difference
扩展其他帖子的想法。我喜欢我们可以匹配案例类的解决方案。以下代码仅返回访问者可能被调用的不同事件的字符串集合。
FileWalker的实现是:
Extending on the ideas of the other posts. I like the solution where we can match over case classes. The following code just returns a collection of strings for the different events that the visitor would have been called on.
The implementation of FileWalker is: