What is the result?

Given the following code fragment:
<code>
public static void main(String[] args) {
Path tempFile = null;
try {
Path p = Paths.get(“emp”);
tempFile = Files.createTempFile(p, “report”, “.tmp”);
try (BufferedWriter writer = Files.newBufferedWriter(tempFile, Charset.forName(“UTF8”)))){
writer.write(“Java SE 7”);
}
System.out.println(“Temporary file write done”);
} catch(IOException e) {
System.err.println(“Caught IOException: ” + e.getMessage());
}
</code>
What is the result?

Given the following code fragment:

public static void main(String[] args) {
Path tempFile = null;
try {
Path p = Paths.get("emp");
tempFile = Files.createTempFile(p, "report", ".tmp");
try (BufferedWriter writer = Files.newBufferedWriter(tempFile, Charset.forName("UTF8")))){
writer.write("Java SE 7");
}
System.out.println("Temporary file write done");
} catch(IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}

What is the result?

A.
The report.tmp file is purged during reboot.

B.
The report.tmp file is automatically purged when it is closed.

C.
The report.tmp file exists until it is explicitly deleted.

D.
The report.tmp file is automatically purged when the execution of the program completes.

Explanation:
The createTempFile (String prefix,
String suffix,
FileAttribute<?>… attrs) method creates an empty file in the default temporary-file directory, using
the given prefix and suffix to generate its name.
This method is only part of a temporary-file facility. Where used as a work files, the resulting file
may be opened using the DELETE_ON_CLOSE option so that the file is deleted when the
appropriate close method is invoked. Alternatively, a shutdown-hook, or the File.deleteOnExit()
mechanism may be used to delete the file automatically.
In this scenario no delete mechanism is specified.
Reference: java.nio.file.createTempFile



Leave a Reply 0

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