nerdexam
Oracle

1Z0-805 · Question #8

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…

The correct answer is C. The report.tmp file exists until it is explicitly deleted. The createTempFile (String prefix, 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…

Java I/O Fundamentals

Question

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()); } Oracle 1Z0-805 Exam } What is the result?

Options

  • AThe report.tmp file is purged during reboot.
  • BThe report.tmp file is automatically purged when it is closed.
  • CThe report.tmp file exists until it is explicitly deleted.
  • DThe report.tmp file is automatically purged when the execution of the program completes.

How the community answered

(54 responses)
  • A
    6% (3)
  • B
    11% (6)
  • C
    80% (43)
  • D
    4% (2)

Explanation

The createTempFile (String prefix, 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.

Topics

#createTempFile#temp file lifecycle#NIO.2#BufferedWriter

Community Discussion

No community discussion yet for this question.

Full 1Z0-805 Practice