基于变量读取某些文件夹



我有以下文件夹结构:

/mnt/tmp/fldr1
/mnt/tmp/fldr2
/mnt/tmp/fldr3
/mnt/tmp/fldr4
/mnt/tmp/fldr5
/mnt/tmp/fldr6

基于一个变量,我将从某些文件夹中读取镶木地板文件。

eg. 
if trail = 2 I would read only fldr1 and fldr2
if trail = 4 I would read only fldr1, fldr2, fldr3 and fldr4
if trail = 6 I would read all the folders

我有下面的代码。

scala> val cnt = 2
val cnt: Int = 2
scala> val xs = List(1, cnt)
val xs: List[Int] = List(1, 2)
scala> val ys = List("/mnt/tmp1", "/mnt/tmp2")
val ys: List[String] = List(/mnt/tmp1, /mnt/tmp2)
scala> val cross = xs.flatMap(x => ys.map(y => (y + "/trial=" + x.toString)))
val cross: List[String] = List(/mnt/tmp1/trial=1, /mnt/tmp2/trial=1, /mnt/tmp1/trial=2, /mnt/tmp2/trial=2)

然后,我会仔细阅读路径,并获得镶木地板文件。

还有其他更简单的方法吗?

基本骨架可以是:

val dirsstr="/mnt/tmp/fldr1,/mnt/tmp/fldr2,/mnt/tmp/fldr3,/mnt/tmp/fldr4,/mnt/tmp/fldr5,/mnt/tmp/fldr6"
val dirs = dirsstr.split(",")
//get an random generator - you will have your own input
val trail = scala.util.Random
trail.nextInt(6) match{
case 2 => {
readFiles(dirs(0));
readFiles(dirs(1));
}
case 4 => for(i <- (0 to 3)) readFiles(dirs(i));
case 6 => dirs.foreach(dir => readFiles(dir))
case _ => println("Out of scope of your question :)")
}

其中readFiles取决于你的火花、镶木地板背景。当然,还有很多其他(也许更好(的方法可以做到这一点,

相关内容

  • 没有找到相关文章

最新更新