Secure temporary file: Difference between revisions

m
(Realize in F#)
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by one other user not shown)
Line 326:
Alternative example
<syntaxhighlight lang="java">
import java.io.FileBufferedWriter;
import java.ionio.Filefile.Files;
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 {
// Create a temporary file in the directory D:\.
// 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.
valPath tftemporaryFilePath = FileFiles.createTempFile(Path.of("D:/"), "tempexample", ".tmp");
// Invoking the following command will cause the file to be deleted when this program exits normally,
 
// otherwise the file will persist.
// For securityuniqueness, the Java API will insert a random number between the file name and the file extension.given prefix
temporaryFile.deleteOnExit();
// and the file extension.
System.out.println("Temporary file created: " + temporaryFile.getName()temporaryFilePath);
// For security, Java will insert a random number between the file name and the file extension.
 
if ( temporaryFile.exists() ) {
// InvokingOpening it with the following commandoption will cause the file to be deleted when this programit exitsis normally,closed.
System.out.println("Temporary file created: " + temporaryFile.getName());
BufferedWriter tempFileWriter = Files.newBufferedWriter(
} else {
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 392 ⟶ 395:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">//import version 1kotlin.io.1path.2createTempFile
import kotlin.io.path.deleteExisting
 
fun main() {
import java.io.File
val File temporaryFiletempFilePath = File.createTempFile("example", ".tmp", new File("D:/"));
 
System.out.println("Temporary file cannotcreated: be 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 902 ⟶ 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,476

edits