public class UniqueFileNameBuilder extends Object
Example usage:
// Create a unique JPEG file name in the "Pictures" directory. UniqueFileNameBuilder builder = new UniqueFileNameBuilder(); builder.setDirectory(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)); builder.setFileNameFormat("Picture %d"); builder.setFileExtension("jpg"); java.io.File imageFile = builder.build(); if (imageFile != null) { // Unique file name was generated. Create the file here. }
Constructor and Description |
---|
UniqueFileNameBuilder()
Create a new file name builder object.
|
Modifier and Type | Method and Description |
---|---|
File |
build()
Generates a unique file name for the directory that you provided to the setDirectory() method.
|
File |
getDirectory()
Gets the directory this builder will generate a unique file name in.
|
String |
getFileExtension()
Gets the file extension to be appended to the file name.
|
String |
getFileNameFormat()
Gets the file name format string that this builder will use to generate a unique file name.
|
void |
setDirectory(File directory)
Sets the directory to generate a unique file name in.
|
void |
setFileExtension(String extension)
Sets the file extension to be appended to the file name.
|
void |
setFileNameFormat(String format)
Sets the file name format string to be used to genearte a unique file name.
|
public UniqueFileNameBuilder()
public void setDirectory(File directory)
directory
- The directory's path. This directory is expected to exist
or else this object will fail to build a unique file name.public File getDirectory()
Returns null if the directory has not been set yet.
public void setFileNameFormat(String format)
format
- The format string to be used, such as "File %d". This string is expected to have
a "%d" in it so that this builder can generate a unique number for the file name.
This string is not expected to provide a file extension.public String getFileNameFormat()
public void setFileExtension(String extension)
extension
- The extension to be appended to the file name.
Set to null or empty string to not append an extension to the file name.
public String getFileExtension()
Returns an empty string if an extension will not be appended.
public File build()
Note that this method does not actually create a file in the set directory. This method only generates a unique file name that you can use to create a new file with.
You are expected to call the setDirectory(), setFileNameFormat(), and setFileExtension() methods before calling this build() method.
Returns null if the directory does not exist.