Secure temporary file: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(New post.) |
m (→{{header|Wren}}: Minor tidy) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 203:
<syntaxhighlight lang="lisp">(make-temp-file "prefix")
;; => "/tmp/prefix25452LPe"</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
printfn $"%s{System.IO.Path.GetTempFileName()}"
</syntaxhighlight>
{{out}}
<pre>
/tmp/tmpEuSgiY.tmp
</pre>
For the cynical who may not believe the file has been created
</pre>
nigel@nigel:/tmp$ ls *.tmp
tmpEuSgiY.tmp
</pre>
=={{header|Fortran}}==
Line 311 ⟶ 326:
Alternative example
<syntaxhighlight lang="java">
import java.io.
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
public final class SecureTemporaryFile {
public static void main(String[] args) throws IOException {
// We should use java.nio.file.Files instead of the old java.io.File, as it is more secure.
File temporaryFile = File.createTempFile("example", ".tmp", new File("D:/"));▼
// If the file cannot be created, it will throw an exception.
// Invoking the following command will cause the file to be deleted when this program exits normally,▼
// For
// and the file extension.
▲ // For security, Java will insert a random number between the file name and the file extension.
▲ //
▲ System.out.println("Temporary file created: " + temporaryFile.getName());
BufferedWriter tempFileWriter = Files.newBufferedWriter(
temporaryFilePath, StandardOpenOption.DELETE_ON_CLOSE);
System.out.println("Temporary file cannot be created.");▼
// ... write to file, read it back in, close it...
}
}
</syntaxhighlight>
{{ out }}
<pre>
Temporary file created: D:\example12312088502442779987.tmp
</pre>
Line 377 ⟶ 395:
=={{header|Kotlin}}==
<syntaxhighlight lang="
import kotlin.io.path.deleteExisting
fun main() {
▲import java.io.File
tempFilePath.deleteExisting()
▲ val tf = File.createTempFile("temp", ".tmp")
}</syntaxhighlight>
Sample output
{{out}}
<pre>
Temporary file created: /tmp/example14437465325231438926.tmp
</pre>
Line 887 ⟶ 899:
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
import "./ioutil" for File, FileUtil
import "./fmt" for Fmt
var rand = Random.new()
|