Deletion File in Scala

Good morning,

Could you kindly help me please ? I am trying to delete a specific file in Scala, however all the possibilities that I have tried did not seem to work out. For example :

new File(filePath).delete()
new File(filePath).deleteOnExit()

Thanks a lot and good day :slight_smile:

What does “doesn’t work” means?

Did it not compiled? Did it failed with a runtime exception? Did it just didn’t deleted the file?

I don’t see anything wrong with the code. Are you sure filePath is pointing where you think it is? I created directory /tmp/foo. Touched file /tmp/foo/xyz. Then ran this code after which file xyz was deleted.

package foo

import java.io._

object Test {
  def main(av: Array[String]): Unit =
    new File("/tmp/foo/xyz").delete()
}
2 Likes