本文整理汇总了Scala中java.nio.file.NoSuchFileException类的典型用法代码示例。如果您正苦于以下问题:Scala NoSuchFileException类的具体用法?Scala NoSuchFileException怎么用?Scala NoSuchFileException使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了NoSuchFileException类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: DirectoryToLoad
//设置package包名称以及导入依赖的类
package pl.com.bms.fileSynchronizer.config
import java.io.File
import java.nio.file.{NoSuchFileException, Paths}
case class DirectoryToLoad(sourceRoot: String, path: String) {
def filesToLoad(): List[String] = {
val absolutePath = sourceRoot + path
val rootDirectory = findDirectory(absolutePath) getOrElse {
throw new NoSuchFileException(absolutePath)
}
deepListFiles(rootDirectory)
.map(file => Paths.get(sourceRoot).relativize(file.toPath).toString)
}
def findDirectory(path: String): Option[java.io.File] = {
val directory = new File(path)
if(directory.exists) Some(directory) else None
}
def deepListFiles(file: java.io.File) : List[java.io.File] = {
file
.listFiles
.flatMap(file => if(file.isDirectory) file :: deepListFiles(file) else List(file))
.toList
}
}
示例2: DirectoryToLoadTest
//设置package包名称以及导入依赖的类
package pl.com.bms.fileSynchronizer
import java.nio.file.NoSuchFileException
import org.scalatest.{BeforeAndAfter, Matchers, WordSpec}
import pl.com.bms.fileSynchronizer.config.DirectoryToLoad
import java.io.File
class DirectoryToLoadTest extends WordSpec with Matchers with BeforeAndAfter {
val TEST_RESOURCES_BASE = "src/test/resources"
val EMPTY_DIRECTORY_PATH = "/emptyDirectory"
"DirectoryToLoad" when {
"created on empty directory" should {
before {
new File(TEST_RESOURCES_BASE + EMPTY_DIRECTORY_PATH).mkdirs()
}
"return an empty list for empty folder" in {
val directory = DirectoryToLoad(TEST_RESOURCES_BASE, EMPTY_DIRECTORY_PATH)
directory.filesToLoad() shouldBe empty
}
after {
new File(TEST_RESOURCES_BASE + EMPTY_DIRECTORY_PATH).delete()
}
}
"created on non-existent directory" should {
"throw an exception for non-existent folder" in {
intercept[NoSuchFileException] {
DirectoryToLoad(TEST_RESOURCES_BASE, "/do-not-exist").filesToLoad()
}
}
}
"created on non-empty directory" should {
"return a deep list of files and folders" in {
val directory = DirectoryToLoad(TEST_RESOURCES_BASE, "/")
directory.filesToLoad() should contain allOf (
"testDirectory",
"testDirectory/testDirectory",
"testDirectory/testDirectory/testFile",
"testDirectory/testFile"
)
}
}
}
}