Recursively create directory

前端 未结 10 587
萌比男神i
萌比男神i 2020-12-30 23:20

Does anyone know how to use Java to create sub-directories based on the alphabets (a-z) that is n levels deep?

 /a
    /a
        /a
        /b
        /c
           


        
10条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 23:54

    Scala code:

      def makePathRecursive(path: String) = {
        import java.io.File
        import scala.util.{Try, Failure, Success}
    
        val pathObj = new File(path)
        pathObj.exists match {
          case true => // do nothing
          case false => Try(pathObj.mkdirs) match {
            case Success(_) => // it worked 
            case Failure(e) => // maybe created meanwhile by another thread
              pathObj.exists match { 
              case false => throw new Exception(e)
              case _ =>  
            }
          }
        }
      }
    

提交回复
热议问题