This article contains about to Scala Create Temporary File using java.io.File and java.nio.file.Files

 Scala Create Temporary File using java.io.File

java.io.File has createTempFile method which accept two arguments prefix and postfix of file to create temporary. 

import java.io.File

object ScalaCreateTempFile {
  def main (args: Array[String]): Unit = {
    val file = File.createTempFile("pre-",".txt");      // val means final, now file reference can not be change
    print(file.toPath)    // it will print file path
  }
}

Output:

C:\Users\JavaDeveloperZone\AppData\Local\Temp\pre-8848350074072709177.txt

Scala Create Temporary File using java.nio.file.Files

java.nio.file.Files has createTempFile method which accept two arguments prefix and postfix of file to create temporary. 

import java.nio.file.Files

object ScalaCreateTempFile {
  def main (args: Array[String]): Unit = {
    val path = Files.createTempFile("pre-",".txt");      // val means final, now file reference can not be change
    print(path)    // it will print file path
  }
}

Output:

C:\Users\JavaDeveloperZone\AppData\Local\Temp\pre-8848350074072709177.txt

 

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No

Leave a Reply

Your email address will not be published. Required fields are marked *