Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java File createTempFile() Method

Java File createTempFile() Method

Java File Java File


createTempFile() is a method in Java's java.io.File a static method provided by the class, used to create a temporary file in a specified directory. This method is very useful, especially when you need a file for temporary data storage; it ensures the uniqueness of the file name and avoids conflicts with other files.

Method Syntax

createTempFile() The method has three overloaded versions:

public

static

File

createTempFile

(

String

prefix,

String

suffix

)

throws

IOException

public

static

File

createTempFile

(

String

prefix,

String

suffix,

File

directory

)

throws

IOException

// Added in Java 7 and above

public

static

File

createTempFile

(

String

prefix,

String

suffix,

File

directory, FileAttribute

...

attrs

)

throws

IOException

Parameter Description

1. Basic parameters

2. New parameters added in Java 7

Return Value

The method returns a File object, representing the newly created temporary file.


Usage examples

Example 1: Create a default temporary file

Example

import

java.io.File

;

import

java.io.IOException

;

public

class

TempFileExample

{

   

public

static

void

main

(

String

[

]

args

)

{

       

try

{

           

// Create a temporary file (using the default temporary directory)

           

File

tempFile

=

File

.

createTempFile

(

"example"

,

".tmp"

)

;

           

System

.

out

.

println

(

"Temporary file path: "

tempFile.

getAbsolutePath

(

)

)

;

           

           

// Automatically delete on program exit

            tempFile.

deleteOnExit

(

)

;

       

}

catch

(

IOException

e

)

{

            e.

printStackTrace

(

)

;

       

}

   

}

}

Example 2: Create a temporary file in a specified directory

Example

import

java.io.File

;

import

java.io.IOException

;

public

class

TempFileExample2

{

   

public

static

void

main

(

String

[

]

args

)

{

       

try

{

           

// Specified directory

           

File

dir

=

new

File

(

"C:/temp"

)

;

           

           

// Create a temporary file in the specified directory

           

File

tempFile

=

File

.

createTempFile

(

"data"

,

".csv"

, dir

)

;

           

System

.

out

.

println

(

"Temporary file path: "

tempFile.

getAbsolutePath

(

)

)

;

       

}

catch

(

IOException

e

)

{

            e.

printStackTrace

(

)

;

       

}

   

}

}

Example 3: Set file attributes in Java 7+

Example

import

java.io.File

;

import

java.io.IOException

;

import

java.nio.file.attribute.PosixFilePermission

;

import

java.nio.file.attribute.PosixFilePermissions

;

import

java.util.Set

;

public

class

TempFileExample3

{

   

public

static

void

main

(

String

[

]

args

)

{

       

try

{

           

// Set file permissions (only applicable to POSIX-compatible systems)

            Set

<
PosixFilePermission
perms
=

PosixFilePermissions.

fromString

(

"rw-r-----"

)

;

            FileAttribute

<
Set
<
PosixFilePermission
attr
=

PosixFilePermissions.

asFileAttribute

(

perms

)

;

           

           

// Create a temporary file with specific permissions

           

File

tempFile

=

File

.

createTempFile

(

"secure"

,

".dat"

,

null

, attr

)

;

           

System

.

out

.

println

(

"Secure temporary file path: "

tempFile.

getAbsolutePath

(

)

)

;

       

}

catch

(

IOException

e

)

{

            e.

printStackTrace

(

)

;

       

}

   

}

}


Notes

  1. File name uniqueness: the method automatically generates a unique file name, ensuring no conflict with existing files
  2. File deletion: temporary files are not deleted automatically; you need to call deleteOnExit() or delete them manually
  3. Prefix length: the prefix parameter must be at least 3 characters long, otherwise it throws IllegalArgumentException
  4. Directory permissions: ensure you have write permission for the specified directory
  5. Cross-platform compatibility: file attribute settings may behave differently on different operating systems

Best Practices

  1. Always handle the that may be thrown IOException
  2. Consider using try-with-resources statement to manage temporary files
  3. For sensitive data, ensure appropriate file permissions are set
  4. Define a clear cleanup strategy for temporary files (automatic or manual deletion)

Example

// Example using try-with-resources

try

{

    Path tempFile

=

Files.

createTempFile

(

"example"

,

".tmp"

)

;

   

try

(

BufferedWriter

writer

=

Files.

newBufferedWriter

(

tempFile

)

)

{

        writer.

write

(

"Temporary file content"

)

;

   

}

   

// Automatically deleted after use

    Files.

deleteIfExists

(

tempFile

)

;

}

catch

(

IOException

e

)

{

    e.

printStackTrace

(

)

;

}

By using properly createTempFile() method, you can safely and efficiently manage temporary files in Java programs.

Java File Java File