How to move file/files from one folder to another in Scala

后端 未结 3 970
野的像风
野的像风 2021-01-21 02:54

I am new to Scala.

I\'ve Googled a lot on how to move files in Scala, but have only found how to move files in Java. I tried to move files using Java import Java.io

3条回答
  •  情书的邮戳
    2021-01-21 03:52

    Refer the below Scala code from my Github : https://github.com/saghircse/SelfStudyNote/blob/master/Scala/MoveRenameFiles.scala

    You need to specify your source and target folder in main function:

        val src_path = "" // Give your source directory
        val tgt_path = "" // Give your target directory
    

    It also rename files. If you do not want to rename files, then update as below:

    val FileList=getListOfFiles(src_path)
    FileList.foreach{f => 
      val src_file = f.toString()
    
      //val tgt_file = tgt_path + "/" + getFileNameWithTS(f.getName) // If you want to rename files in target
      val tgt_file = tgt_path + "/" + f.getName // If you do not want to rename files in target
    
      copyRenameFile(src_file,tgt_file)
      //moveRenameFile(src_file,tgt_file) - Try this if you want to delete source file
      println("File Copied : " + tgt_file)
    }
    

提交回复
热议问题