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

后端 未结 3 968
野的像风
野的像风 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:48

    From my understanding of your question,
    you are trying to pass String variables instead of java.nio.Path variables to the Files.move().

    The below way works:

    import java.io.File
    import java.nio.file.{Files, Path, StandardCopyOption}
    
    val d1 = new File("/abcd").toPath
    val d2 = new File("/efgh").toPath
    
    Files.move(d1, d2, StandardCopyOption.ATOMIC_MOVE)
    

    However I see one more issue in your code. Both StandardCopyOption.REPLACE_EXISTING and StandardCopyOption.ATOMIC_MOVE should work but, you can't move a parent directory directly into it's child directory.

    $ mv public/ public/images
    mv: cannot move ‘public/’ to a subdirectory of itself, ‘public/images’
    

    Instead you might want to move public -> tmp -> public/images

提交回复
热议问题