6

I am trying to create a temp directory and file underneath it. Here is my code snippet:

var tempPath = System.getProperty("java.io.tmpdir")
val myDir = new File(tempPath.concat(scala.util.Random.nextString(10).toString))
myDir.mkdir()

val tempFile = new File(myDir.toString+"/temp.log")

This code is working fine. However I am wondering if there is any better way of doing this, please provide your comments.

Rajish
  • 6,755
  • 4
  • 34
  • 51
Pratap D
  • 311
  • 3
  • 7
  • 14

1 Answers1

4

Java has existing methods that can do this for you, like Files.createTempFile, Files.createTempDirectory and their overloads.

You can find some examples in this blog post.

Hosam Aly
  • 41,555
  • 36
  • 141
  • 182