当前位置:网站首页>Scala 获取指定目录下的所有文件
Scala 获取指定目录下的所有文件
2022-07-22 21:23:00 【武念】
最近在学习Scala,想要获取指定目录下的所有文件名,但是Scala 中有没有相应的库函数,由于本人是新手,所以弄了半天,好不容易才将网上的一段Scala 递归获取指定目录下所有目录的代码改成获取文件名,特在此备忘,也希望高手指点。
下面是一段递归获取目录名称的代码:
-
def
subdirs
(dir: File): Iterator[File] = {
-
val
children
= dir.listFiles.filter(_.isDirectory)
-
children.toIterator ++ children.toIterator.flatMap(subdirs _)
-
}
稍加改动后变成获取文件名的代码,如下,欢迎指正:
-
def
subdirs2
(dir: File): Iterator[File] = {
-
val
d
= dir.listFiles.filter(_.isDirectory)
-
val
f
= dir.listFiles.filter(_.isFile).toIterator
-
f ++ d.toIterator.flatMap(subdirs2 _)
-
}
最后再稍微改改,即可获得指定目录下所有的文件名和目录名:
-
def
subdirs3
(dir: File): Iterator[File] = {
-
val
d
= dir.listFiles.filter(_.isDirectory)
-
val
f
= dir.listFiles.toIterator
-
f ++ d.toIterator.flatMap(subdirs3 _)
-
}
边栏推荐
猜你喜欢

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

Overview of multisensor fusion -- FOV and bev

多传感器融合综述---FOV与BEV

6-15漏洞利用-smb-RCE远程命令执行

Information system project managers must recite the core examination points (49) contract law

“蔚来杯“2022牛客暑期多校训练营1 (部分题目总结)

无代码生产新模式探索

聊聊并发编程的12种业务场景

UE4引擎的CopyTexture, CopyToResolveTarget

【开发技术】SpingBoot数据库与持久化技术,JPA,MongoDB,Redis
随机推荐
How to make a high-quality VR panorama? Are there any simple ones that can be taken?
Wechat hotel reservation applet graduation project (6) opening defense ppt
A web server where browser users access server files
Classes and objects (1)
Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
Application of the latest version of Ontrack easyrecovery computer data recovery software
我为OpenHarmony 写代码,战“码”先锋第二期正式开启!
如何使用订单流分析工具(下)
《postgresql指南--内幕探索》第二章 进程与内存架构
微信小程序项目实战
2022年暑假ACM热身练习4(总结)
Wechat campus second-hand book trading applet graduation design finished product (5) assignment
6-14漏洞利用-rpcbind漏洞利用
Educational Codeforces Round 132 A - D
11.37万的星瑞是怎样一个产品和表现力?一起来看看吧
Chapter 2 how to use sourcetree to update code locally
Mysql无法访问,navicat提示:is not allowed to connect to this MySQL server
How to use the order flow analysis tool (in)
局域网SDN技术硬核内幕 8 从二层交换到三层路由
BGP笔记(二)