There is a method on the java ‘File’ class named deleteOnExit
.
It seems to be useful when creating tmp files. You can notify the system that you’d like the file deleted when the VM session closes.
I’m using it as follows.
object GraphViz {
import java.io.{OutputStream, File}
def bddView(bdd: Bdd, drawFalseLeaf: Boolean): Unit = {
import sys.process._
val png = bddToPng(bdd,drawFalseLeaf)
val cmd = s"open $png"
cmd.!
png
}
def bddToPng(bdd:Bdd,drawFalseLeaf: Boolean): String = {
val png = File.createTempFile("bdd", ".png")
val pngPath = png.getAbsolutePath
val dot = File.createTempFile("bdd", ".dot")
val dotPath = dot.getAbsolutePath
bddToPng(bdd,dotPath, drawFalseLeaf)
import sys.process._
val cmd = s"""dot -Tpng $dotPath -o $pngPath"""
println(s"cmd = $cmd")
cmd.!
png.deleteOnExit()
dot.deleteOnExit()
pngPath
}
...
I’m creating a .dot
file from Scala code, then running dot
graphviz from UNIX via cmd.!
. This works fine. But when I run open filename
as a shell comment, and the png
file is not found. The problem is that the VM of the java program has already exited.
What is the correct use model which I should use here. Do I need to refactor my code to always comment out the deleteOnExit()
line when running for debug purposes?