Secure temporary file: Difference between revisions

m
(→‎{{header|Java}}: Use java.nio to create temp file to avoid security vulnerability (see javadoc for details))
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by one other user not shown)
Line 340:
Path temporaryFilePath = Files.createTempFile(Path.of("D:/"), "example", ".tmp");
 
// For uniqueness, the Java API will insert a random number between the given prefix and the file extension.
// and the file extension.
System.out.println("Temporary file created: " + temporaryFilePath);
 
// Opening it with the following option will cause the file to be deleted when it is closed.
BufferedWriter tempFileWriter = Files.newBufferedWriter(temporaryFilePath, StandardOpenOption.DELETE_ON_CLOSE);
temporaryFilePath, StandardOpenOption.DELETE_ON_CLOSE);
// ... write to file, read it back in, close it...
}
Line 393 ⟶ 395:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">//import version 1kotlin.io.1path.2createTempFile
import kotlin.io.path.deleteExisting
 
fun main() {
import java.io.File
val tftempFilePath = File.createTempFile("tempexample", ".tmp")
 
println("Temporary file created: $tempFilePath")
fun main(args: Array<String>) {
tempFilePath.deleteExisting()
try {
val tf = File.createTempFile("temp", ".tmp")
println(tf.absolutePath)
tf.delete()
}
catch (ex: Exception) {
println(ex.message)
}
}</syntaxhighlight>
Sample output (Ubuntu v14.04):
{{out}}
<pre>
Temporary file created: /tmp/example14437465325231438926.tmp
/tmp/temp1551492276377305257.tmp
</pre>
 
Line 903 ⟶ 899:
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./ioutil" for File, FileUtil
import "./fmt" for Fmt
 
var rand = Random.new()
9,485

edits