org.schmant.support.io
Class TempFileUtil

java.lang.Object
  extended by org.schmant.support.io.TempFileUtil

public final class TempFileUtil
extends Object

Utility class with static methods for creating temporary files and directories. Files and directories created using the methods in this class are by default deleted when the Schmant program exits.

Since:
0.5
Author:
Karl Gustafsson

Field Summary
static String DEFAULT_PREFIX
          The default name prefix for temporary files and directories.
static String DEFAULT_SUFFIX
          The default name suffix for temporary files and directories.
 
Method Summary
static File createTempDir()
          Create a temporary directory in the default temporary files directory (or the directory referenced by the java.io.tmpdir system property if the default temporary files directory is not set).
static File createTempDir(boolean keep)
          Create a temporary directory in the default temporary files directory (or the directory referenced by the java.io.tmpdir system property if the default temporary files directory is not set).
static File createTempDir(File directory)
          Create a temporary directory in the supplied directory (which must exist).
static File createTempDir(File directory, String prefix, String suffix, boolean keep)
          Create a temporary directory in the given directory named with the supplied directory name prefix and suffix.
static File createTempDir(String prefix, String suffix, boolean keep)
          Create a temporary directory named with the supplied directory name prefix and suffix in the default temporary files directory.
static Directory createTempDirectory()
          Create a temporary directory entity in the default temporary files directory.
static Directory createTempDirectory(boolean keep)
          Create a temporary directory entity in the default temporary files directory.
static DirectoryView createTempDirectory(DirectoryView d)
          Create a temporary directory entity in the supplied directory.
static DirectoryView createTempDirectory(DirectoryView d, String prefix, String suffix, boolean keep)
          Create a temporary directory entity in the supplied directory.
static Directory createTempDirectory(FileSystem fs)
          Create a temporary directory entity in the file system's default temporary files directory.
static Directory createTempDirectory(String prefix, String suffix, boolean keep)
          Create a temporary directory the in the default temporary files directory.
static Directory createTempDirectoryInFSTempFilesDirectoryIfSet(FileSystem fs)
          Create a temporary directory entity.
static EFile createTempEFile()
          Create a read/write temporary file entity in the platform's temporary files directory.
static EFile createTempEFile(DirectoryView d)
          Create a temporary, initially empty file entity in the supplied directory.
static EFile createTempEFile(DirectoryView d, String prefix, String suffix, boolean keep)
          Create a temporary, initially empty file entity in the supplied directory.
static EFile createTempEFile(FileSystem fs)
          Create a temporary, initially empty file entity in the file system's default temporary files directory.
static EFile createTempEFileInFSTempFilesDirectoryIfSet(FileSystem fs)
          Create a temporary, initially empty file entity.
static File createTempFile()
          Create a temporary, initially empty, file in the default temporary files directory (or in the directory referenced by the java.io.tmpdir system property if the default temporary files directory is not set).
static File createTempFile(File directory)
          Create a temporary, initially empty, file in the supplied directory (which must exist).
static File createTempFile(File directory, String prefix, String suffix, boolean keep)
          Create a temporary file in the given directory named with the supplied file name prefix and suffix.
static Object init(boolean keep)
          The Launcher class uses this to initialize a new context for temporary files.
static void setDefaultTempDirectory(Directory d)
          Set a default directory for temporary files.
static void setDefaultTempDirectory(File d)
          Set a default directory for temporary files.
static void setMaxRandomNumber(int max)
          Set the biggest random number (exclusive) to use when creating temporary files and directories.
static void tearDown(Object prevContext)
          Tear down the current temporary files context and restore the old, if any.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_PREFIX

public static final String DEFAULT_PREFIX
The default name prefix for temporary files and directories.

See Also:
Constant Field Values

DEFAULT_SUFFIX

public static final String DEFAULT_SUFFIX
The default name suffix for temporary files and directories.

See Also:
Constant Field Values
Method Detail

init

public static Object init(boolean keep)
The Launcher class uses this to initialize a new context for temporary files. If this class already has a context, it is returned and Launcher will keep it and return it when it calls tearDown(Object).

Parameters:
keep - Should temporary files be kept?
Returns:
The previous temporary files context.
See Also:
tearDown(Object)

tearDown

public static void tearDown(Object prevContext)
Tear down the current temporary files context and restore the old, if any.

This is used by the Launcher class.

Parameters:
prevContext - If init(boolean) returned a temporary files context, it should be passed in here.

setDefaultTempDirectory

public static void setDefaultTempDirectory(Directory d)
Set a default directory for temporary files. If this is set, the directory set here is used instead of the value of the system property java.io.tmpdir for creating temporary entities in. It can be useful, or at least aesthetically pleasing, for big build scripts to put their temporary files in another catalog than java.io.tmpdir since they often generate so many of them.

Use for instance setDefaultTempDirectory(createTempDirectory()) to put all temporary files in a subdirectory of java.io.tmpdir.

Parameters:
d - The temporary files directory.
See Also:
setDefaultTempDirectory(File)

setDefaultTempDirectory

public static void setDefaultTempDirectory(File d)
Set a default directory for temporary files. If this is set, the directory set here is used instead of the value of the system property java.io.tmpdir for creating temporary entities in. It can be useful, or at least aesthetically pleasing, for big build scripts to put their temporary files in another catalog than java.io.tmpdir since they often generate so many of them.

Use for instance setDefaultTempDirectory(createTempDirectory()) to put all temporary files in a subdirectory of java.io.tmpdir.

Parameters:
d - The temporary files directory.
See Also:
setDefaultTempDirectory(Directory)

setMaxRandomNumber

public static void setMaxRandomNumber(int max)
                               throws IllegalArgumentException
Set the biggest random number (exclusive) to use when creating temporary files and directories.

Parameters:
max - The biggest random number. Must be > 0.
Throws:
IllegalArgumentException - If the number is <= 0.

createTempDir

public static File createTempDir()
Create a temporary directory in the default temporary files directory (or the directory referenced by the java.io.tmpdir system property if the default temporary files directory is not set). The directory and all its contents will be deleted when Schmant exits.

Returns:
A directory.
See Also:
setDefaultTempDirectory(File)

createTempDir

public static File createTempDir(boolean keep)
Create a temporary directory in the default temporary files directory (or the directory referenced by the java.io.tmpdir system property if the default temporary files directory is not set). If keep is set to true, the directory will be kept when Schmant exits, otherwise it and all of its contents will be deleted.

Parameters:
keep - Should the directory be kept after Schmant exits?
Returns:
A directory.
Since:
0.9.1
See Also:
setDefaultTempDirectory(File)

createTempDir

public static File createTempDir(File directory)
Create a temporary directory in the supplied directory (which must exist). The created directory and all its contents will be deleted when Schmant exits.

Parameters:
directory - The directory to create the temporary directory in. This directory must exist. Set to null to use the default temporary files directory (the java.io.tmpdir system property).
Returns:
The created directory.
See Also:
setDefaultTempDirectory(File)

createTempDir

public static File createTempDir(String prefix,
                                 String suffix,
                                 boolean keep)
Create a temporary directory named with the supplied directory name prefix and suffix in the default temporary files directory. If keep is set to true, the directory and its contents will not be deleted when the Schmant program exits (unless the directory is created in or in a subdirectory of a temporary files directory that does not have its keep flag set).

Parameters:
prefix - The directory name prefix. Set to null to use the default prefix ("schmant").
suffix - The directory name suffix. Set to null to use the default suffix ("tmp").
keep - If set to true, the directory and its contents will not be deleted when the Schmant program exits. (Unless the file is created in or in a subdirectory of a temporary directory that does not have its keep flag set.)
Returns:
The created directory.
See Also:
createTempDir(), setDefaultTempDirectory(File)

createTempDir

public static File createTempDir(File directory,
                                 String prefix,
                                 String suffix,
                                 boolean keep)
Create a temporary directory in the given directory named with the supplied directory name prefix and suffix. If keep is set to true, the directory and its contents will not be deleted when the Schmant program exits (unless the directory is created in or in a subdirectory of a temporary files directory that does not have its keep flag set).

Parameters:
directory - The directory to create the temporary directory in. This directory must exist. Set to null to use the default temporary files directory.
prefix - The directory name prefix. Set to null to use the default prefix ("schmant").
suffix - The directory name suffix. Set to null to use the default suffix ("tmp").
keep - If set to true, the directory and its contents will not be deleted when the Schmant program exits. (Unless the file is created in or in a subdirectory of a temporary directory that does not have its keep flag set.)
Returns:
The created directory.
See Also:
setDefaultTempDirectory(File)

createTempFile

public static File createTempFile()
Create a temporary, initially empty, file in the default temporary files directory (or in the directory referenced by the java.io.tmpdir system property if the default temporary files directory is not set). The file will be deleted when Schmant exits.

Returns:
An empty file.
See Also:
setDefaultTempDirectory(File)

createTempFile

public static File createTempFile(File directory)
Create a temporary, initially empty, file in the supplied directory (which must exist). The created file will be deleted when Schmant exits.

Parameters:
directory - The directory to create the temporary file in. This directory must exist. Set to null to use the default temporary files directory (the java.io.tmpdir system property).
Returns:
The created, empty file.
See Also:
setDefaultTempDirectory(File)

createTempFile

public static File createTempFile(File directory,
                                  String prefix,
                                  String suffix,
                                  boolean keep)
Create a temporary file in the given directory named with the supplied file name prefix and suffix. If keep is set to true, the file will not be deleted when the Schmant program exits (unless the file is created in, or in a subdirectory of, a temporary directory that does not have its keep flag set).

Parameters:
directory - The directory to create the temporary file in. This directory must exist. Set to null to use the default temporary files directory (the java.io.tmpdir system property).
prefix - The file name prefix. Set to null to use the default prefix ("schmant").
suffix - The file name suffix. Set to null to use the default suffix ("tmp").
keep - If set to true, the file will not be deleted when the Schmant program exits. (Unless the file is created in or in a subdirectory of a temporary directory that does not have its keep flag set.)
Returns:
The created file.
See Also:
setDefaultTempDirectory(File)

createTempEFile

public static EFile createTempEFile(FileSystem fs)
Create a temporary, initially empty file entity in the file system's default temporary files directory. (The file system must have its default temporary files directory set.) The file will be deleted when the Schmant program exits.

Parameters:
fs - The file system.
Returns:
The created file.
See Also:
setDefaultTempDirectory(Directory)

createTempEFileInFSTempFilesDirectoryIfSet

public static EFile createTempEFileInFSTempFilesDirectoryIfSet(FileSystem fs)
Create a temporary, initially empty file entity. Create it in the file system's default temporary files directory, if it is set. Otherwise create the file in the platform's default temporary files directory. The file will be deleted when the Schmant program exits.

Parameters:
fs - The file system.
Returns:
The created file.
See Also:
setDefaultTempDirectory(Directory)

createTempEFile

public static EFile createTempEFile(DirectoryView d)
Create a temporary, initially empty file entity in the supplied directory. The file will be deleted when the Schmant program exits.

Parameters:
d - The directory.
Returns:
The created file.
See Also:
setDefaultTempDirectory(Directory)

createTempEFile

public static EFile createTempEFile(DirectoryView d,
                                    String prefix,
                                    String suffix,
                                    boolean keep)
Create a temporary, initially empty file entity in the supplied directory. If the keep flag is set to true, the file will not be deleted when the Schmant program exits (unless the file is created in, or in a subdirectory of, a temporary files directory that does not have its keep flag set).

Parameters:
d - The directory.
prefix - The file name prefix. Set to null to use the default prefix ("schmant").
suffix - The file name suffix. Set to null to use the default suffix ("tmp").
keep - If set to true, the file will not be deleted when the Schmant program exits (unless the file is created in, or in a subdirectory of, a temporary files directory that does not have its keep flag set).
Returns:
The created file.
See Also:
setDefaultTempDirectory(Directory)

createTempEFile

public static EFile createTempEFile()
Create a read/write temporary file entity in the platform's temporary files directory. The file will be deleted when the Schmant program exits.

Returns:
A temporary file entity.
See Also:
setDefaultTempDirectory(Directory)

createTempDirectory

public static Directory createTempDirectory()
Create a temporary directory entity in the default temporary files directory. The directory with all its contents will be deleted when the Schmant program exits.

See Also:
setDefaultTempDirectory(Directory)

createTempDirectory

public static Directory createTempDirectory(boolean keep)
Create a temporary directory entity in the default temporary files directory. If keep is set to true, the directory will be kept when Schmant exits. Otherwise it and all of its contents will be deleted. Schmant program exits.

Since:
0.9.1
See Also:
setDefaultTempDirectory(Directory)

createTempDirectory

public static Directory createTempDirectory(String prefix,
                                            String suffix,
                                            boolean keep)
Create a temporary directory the in the default temporary files directory. If the keep flag is set to true, the directory and its contents will not be deleted when the Schmant program exits (unless the directory is created in, or in a subdirectory of, a temporary files directory that does not have its keep flag set)..

Parameters:
prefix - The directory name prefix. Set to null to use the default prefix ("schmant").
suffix - The directory name suffix. Set to null to use the default suffix ("tmp").
keep - If set to true, the directory and its contents will not be deleted when the Schmant program exits (unless the directory is created in, or in a subdirectory of, a temporary files directory that does not have its keep flag set)..
Returns:
The created directory.
See Also:
setDefaultTempDirectory(Directory)

createTempDirectory

public static Directory createTempDirectory(FileSystem fs)
Create a temporary directory entity in the file system's default temporary files directory. (The file system must have its default temporary files directory set.) The file will be deleted when the Schmant program exits.

Parameters:
fs - The file system.
Returns:
The created directory.
See Also:
setDefaultTempDirectory(Directory)

createTempDirectoryInFSTempFilesDirectoryIfSet

public static Directory createTempDirectoryInFSTempFilesDirectoryIfSet(FileSystem fs)
Create a temporary directory entity. Create it in the file system's default temporary files directory, if it is set. Otherwise create the directory in the platform's default temporary files directory. The file will be deleted when the Schmant program exits.

Parameters:
fs - The file system.
Returns:
The created directory.
See Also:
setDefaultTempDirectory(Directory)

createTempDirectory

public static DirectoryView createTempDirectory(DirectoryView d)
Create a temporary directory entity in the supplied directory. The created directory and all its contents will be deleted when the Schmant program exits.

Parameters:
d - The directory to create the temporary directory in.
Returns:
The created directory.
See Also:
setDefaultTempDirectory(Directory)

createTempDirectory

public static DirectoryView createTempDirectory(DirectoryView d,
                                                String prefix,
                                                String suffix,
                                                boolean keep)
Create a temporary directory entity in the supplied directory. If the keep flag is set to true, the directory and its contents will not be deleted when the Schmant program exits (unless the directory is created in, or in a subdirectory of, a temporary files directory that does not have its keep flag set)..

Parameters:
d - The directory.
prefix - The directory name prefix. Set to null to use the default prefix ("schmant").
suffix - The directory name suffix. Set to null to use the default suffix ("tmp").
keep - If set to true, the directory and its contents will not be deleted when the Schmant program exits(unless the directory is created in, or in a subdirectory of, a temporary files directory that does not have its keep flag set)..
Returns:
The created directory.
See Also:
setDefaultTempDirectory(Directory)