APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.files

public Class FileHandle

extends Object
Class Inheritance
Known Direct Subclasses
com.badlogic.gdx.files.FileHandleStream
Imports
java.io.BufferedInputStream, .BufferedOutputStream, .BufferedReader, .File, .FileFilter, .FileInputStream, .FileOutputStream, .FilenameFilter, .IOException, .InputStream, .InputStreamReader, .OutputStream, .OutputStreamWriter, .RandomAccessFile, .Reader, .UnsupportedEncodingException, .Writer, java.nio.ByteBuffer, .ByteOrder, java.nio.channels.FileChannel, .FileChannel.MapMode, com.badlogic.gdx.Files, .Files.FileType, .Gdx, com.badlogic.gdx.utils.GdxRuntimeException, .StreamUtils

Represents a file or directory on the filesystem, classpath, Android app storage, or Android assets directory. FileHandles are created via a Files instance. Because some of the file types are backed by composite files and may be compressed (for example, if they are in an Android .apk or are found via the classpath), the methods for extracting a path() or file() may not be appropriate for all types. Use the Reader or Stream methods here to hide these dependencies from your platform independent code.
Authors
mzechner, Nathan Sweet

Field Summary

Modifier and TypeField and Description
protected File
protected Files.FileType

Constructor Summary

AccessConstructor and Description
protected
public
FileHandle(String
the filename.
fileName
)

Creates a new absolute FileHandle for the file name.

public
FileHandle(File
the file.
file
)

Creates a new absolute FileHandle for the File.

protected
FileHandle(String fileName, Files.FileType type)

protected

Method Summary

Modifier and TypeMethod and Description
public FileHandle
child(String name)

Returns a handle to the child with the specified name.

private static void
copyDirectory(FileHandle sourceDir, FileHandle destDir)

private static void
public void
copyTo(FileHandle dest)

Copies this file or directory to the specified file or directory.

public boolean
delete()

Deletes this file or empty directory and returns success.

public boolean
deleteDirectory()

Deletes this file or directory and all children, recursively.

private static boolean
public void
emptyDirectory()

Deletes all children of this directory, recursively.

public void
emptyDirectory(boolean preserveTree)

Deletes all children of this directory, recursively.

private static void
emptyDirectory(File file, boolean preserveTree)

public boolean
equals(Object obj)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

private int
public boolean
exists()

Returns true if the file exists.

public String
extension()

Returns the file extension (without the dot) or an empty string if the file name doesn't contain a dot.

public File
file()

Returns a java.io.File that represents this file handle.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public boolean
isDirectory()

Returns true if this file is a directory.

public long
lastModified()

Returns the last modified time in milliseconds for this file.

public long
length()

Returns the length in bytes of this file, or 0 if this file is a directory, does not exist, or the size cannot otherwise be determined.

public FileHandle[]
list()

Returns the paths to the children of this directory.

public FileHandle[]
list(FileFilter
the FileFilter to filter files
filter
)

Returns the paths to the children of this directory that satisfy the specified filter.

public FileHandle[]
list(FilenameFilter
the FilenameFilter to filter files
filter
)

Returns the paths to the children of this directory that satisfy the specified filter.

public FileHandle[]
list(String suffix)

Returns the paths to the children of this directory with the specified suffix.

public ByteBuffer
map()

Attempts to memory map this file in READ_ONLY mode.

public ByteBuffer
map(FileChannel.MapMode mode)

Attempts to memory map this file.

public void
public void
moveTo(FileHandle dest)

Moves this file to the specified file, overwriting the file if it already exists.

public String
name()

Returns: the name of the file, without any parent paths.

public String
nameWithoutExtension()

Returns: the name of the file, without parent paths or the extension.

public FileHandle
public String
path()

Returns: the path of the file as specified on construction, e.g. Gdx.files.internal("dir/file.png") -> dir/file.png.

public String
pathWithoutExtension()

Returns: the path and filename without the extension, e.g. dir/dir2/file.png -> dir/dir2/file.

public InputStream
read()

Returns a stream for reading this file as bytes.

public BufferedInputStream
read(int bufferSize)

Returns a buffered stream for reading this file as bytes.

public byte[]
readBytes()

Reads the entire file into a byte array.

public int

Returns:

the number of read bytes
readBytes
(byte[]
the array to load the file into
bytes
,
int
the offset to start writing bytes
offset
,
int
the number of bytes to read, see length()
size
)

Reads the entire file into the byte array.

public Reader
reader()

Returns a reader for reading this file as characters the platform's default charset.

public Reader
reader(String charset)

Returns a reader for reading this file as characters.

public BufferedReader
reader(int bufferSize)

Returns a buffered reader for reading this file as characters using the platform's default charset.

public BufferedReader
reader(int bufferSize, String charset)

Returns a buffered reader for reading this file as characters.

public String
readString()

Reads the entire file into a string using the platform's default charset.

public String
readString(String
If null the default charset is used.
charset
)

Reads the entire file into a string using the specified charset.

public FileHandle
sibling(String name)

Returns a handle to the sibling with the specified name.

public static FileHandle
public static FileHandle
tempFile(String prefix)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public Files.FileType
type()

public OutputStream
write(boolean
If false, this file will be overwritten if it exists, otherwise it will be appended.
append
)

Returns a stream for writing to this file.

public OutputStream
write(boolean
If false, this file will be overwritten if it exists, otherwise it will be appended.
append
,
int
The size of the buffer.
bufferSize
)

Returns a buffered stream for writing to this file.

public void
write(InputStream input, boolean
If false, this file will be overwritten if it exists, otherwise it will be appended.
append
)

Reads the remaining bytes from the specified stream and writes them to this file.

public void
writeBytes(byte[] bytes, boolean
If false, this file will be overwritten if it exists, otherwise it will be appended.
append
)

Writes the specified bytes to the file.

public void
writeBytes(byte[] bytes, int offset, int length, boolean
If false, this file will be overwritten if it exists, otherwise it will be appended.
append
)

Writes the specified bytes to the file.

public Writer
writer(boolean
If false, this file will be overwritten if it exists, otherwise it will be appended.
append
)

Returns a writer for writing to this file using the default charset.

public Writer
writer(boolean
If false, this file will be overwritten if it exists, otherwise it will be appended.
append
,
String
May be null to use the default charset.
charset
)

Returns a writer for writing to this file.

public void
writeString(String string, boolean
If false, this file will be overwritten if it exists, otherwise it will be appended.
append
)

Writes the specified string to the file using the default charset.

public void
writeString(String string, boolean
If false, this file will be overwritten if it exists, otherwise it will be appended.
append
,
String
May be null to use the default charset.
charset
)

Writes the specified string to the file using the specified charset.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

fileback to summary
protected File file
typeback to summary
protected Files.FileType type

Constructor Detail

FileHandleback to summary
protected FileHandle()
FileHandleback to summary
public FileHandle(String fileName)

Creates a new absolute FileHandle for the file name. Use this for tools on the desktop that don't need any of the backends. Do not use this constructor in case you write something cross-platform. Use the Files interface instead.

Parameters
fileName:String

the filename.

FileHandleback to summary
public FileHandle(File file)

Creates a new absolute FileHandle for the File. Use this for tools on the desktop that don't need any of the backends. Do not use this constructor in case you write something cross-platform. Use the Files interface instead.

Parameters
file:File

the file.

FileHandleback to summary
protected FileHandle(String fileName, Files.FileType type)
FileHandleback to summary
protected FileHandle(File file, Files.FileType type)

Method Detail

childback to summary
public FileHandle child(String name)

Returns a handle to the child with the specified name.

copyDirectoryback to summary
private static void copyDirectory(FileHandle sourceDir, FileHandle destDir)
copyFileback to summary
private static void copyFile(FileHandle source, FileHandle dest)
copyToback to summary
public void copyTo(FileHandle dest)

Copies this file or directory to the specified file or directory. If this handle is a file, then 1) if the destination is a file, it is overwritten, or 2) if the destination is a directory, this file is copied into it, or 3) if the destination doesn't exist, mkdirs() is called on the destination's parent and this file is copied into it with a new name. If this handle is a directory, then 1) if the destination is a file, GdxRuntimeException is thrown, or 2) if the destination is a directory, this directory is copied into it recursively, overwriting existing files, or 3) if the destination doesn't exist, mkdirs() is called on the destination and this directory is copied into it recursively.

Exceptions
GdxRuntimeException:
if the destination file handle is a FileType#Classpath or FileType#Internal file, or copying failed.
deleteback to summary
public boolean delete()

Deletes this file or empty directory and returns success. Will not delete a directory that has children.

Exceptions
GdxRuntimeException:
if this file handle is a FileType#Classpath or FileType#Internal file.
deleteDirectoryback to summary
public boolean deleteDirectory()

Deletes this file or directory and all children, recursively.

Exceptions
GdxRuntimeException:
if this file handle is a FileType#Classpath or FileType#Internal file.
deleteDirectoryback to summary
private static boolean deleteDirectory(File file)
emptyDirectoryback to summary
public void emptyDirectory()

Deletes all children of this directory, recursively.

Exceptions
GdxRuntimeException:
if this file handle is a FileType#Classpath or FileType#Internal file.
emptyDirectoryback to summary
public void emptyDirectory(boolean preserveTree)

Deletes all children of this directory, recursively. Optionally preserving the folder structure.

Exceptions
GdxRuntimeException:
if this file handle is a FileType#Classpath or FileType#Internal file.
emptyDirectoryback to summary
private static void emptyDirectory(File file, boolean preserveTree)
equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

estimateLengthback to summary
private int estimateLength()
existsback to summary
public boolean exists()

Returns true if the file exists. On Android, a FileType#Classpath or FileType#Internal handle to a directory will always return false. Note that this can be very slow for internal files on Android!

extensionback to summary
public String extension()

Returns the file extension (without the dot) or an empty string if the file name doesn't contain a dot.

fileback to summary
public File file()

Returns a java.io.File that represents this file handle. Note the returned file will only be usable for FileType#Absolute and FileType#External file handles.

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
isDirectoryback to summary
public boolean isDirectory()

Returns true if this file is a directory. Always returns false for classpath files. On Android, an FileType#Internal handle to an empty directory will return false. On the desktop, an FileType#Internal handle to a directory on the classpath will return false.

lastModifiedback to summary
public long lastModified()

Returns the last modified time in milliseconds for this file. Zero is returned if the file doesn't exist. Zero is returned for FileType#Classpath files. On Android, zero is returned for FileType#Internal files. On the desktop, zero is returned for FileType#Internal files on the classpath.

lengthback to summary
public long length()

Returns the length in bytes of this file, or 0 if this file is a directory, does not exist, or the size cannot otherwise be determined.

listback to summary
public FileHandle[] list()

Returns the paths to the children of this directory. Returns an empty list if this file handle represents a file and not a directory. On the desktop, an FileType#Internal handle to a directory on the classpath will return a zero length array.

Exceptions
GdxRuntimeException:
if this file is an FileType#Classpath file.
listback to summary
public FileHandle[] list(FileFilter filter)

Returns the paths to the children of this directory that satisfy the specified filter. Returns an empty list if this file handle represents a file and not a directory. On the desktop, an FileType#Internal handle to a directory on the classpath will return a zero length array.

Parameters
filter:FileFilter

the FileFilter to filter files

Exceptions
GdxRuntimeException:
if this file is an FileType#Classpath file.
listback to summary
public FileHandle[] list(FilenameFilter filter)

Returns the paths to the children of this directory that satisfy the specified filter. Returns an empty list if this file handle represents a file and not a directory. On the desktop, an FileType#Internal handle to a directory on the classpath will return a zero length array.

Parameters
filter:FilenameFilter

the FilenameFilter to filter files

Exceptions
GdxRuntimeException:
if this file is an FileType#Classpath file.
listback to summary
public FileHandle[] list(String suffix)

Returns the paths to the children of this directory with the specified suffix. Returns an empty list if this file handle represents a file and not a directory. On the desktop, an FileType#Internal handle to a directory on the classpath will return a zero length array.

Exceptions
GdxRuntimeException:
if this file is an FileType#Classpath file.
mapback to summary
public ByteBuffer map()

Attempts to memory map this file in READ_ONLY mode. Android files must not be compressed.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, doesn't exist, or could not be read, or memory mapping fails, or is a FileType#Classpath file.
mapback to summary
public ByteBuffer map(FileChannel.MapMode mode)

Attempts to memory map this file. Android files must not be compressed.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, doesn't exist, or could not be read, or memory mapping fails, or is a FileType#Classpath file.
mkdirsback to summary
public void mkdirs()
Exceptions
GdxRuntimeException:
if this file handle is a FileType#Classpath or FileType#Internal file.
moveToback to summary
public void moveTo(FileHandle dest)

Moves this file to the specified file, overwriting the file if it already exists.

Exceptions
GdxRuntimeException:
if the source or destination file handle is a FileType#Classpath or FileType#Internal file.
nameback to summary
public String name()
Returns:String

the name of the file, without any parent paths.

nameWithoutExtensionback to summary
public String nameWithoutExtension()
Returns:String

the name of the file, without parent paths or the extension.

parentback to summary
public FileHandle parent()
pathback to summary
public String path()
Returns:String

the path of the file as specified on construction, e.g. Gdx.files.internal("dir/file.png") -> dir/file.png. backward slashes will be replaced by forward slashes.

pathWithoutExtensionback to summary
public String pathWithoutExtension()
Returns:String

the path and filename without the extension, e.g. dir/dir2/file.png -> dir/dir2/file. backward slashes will be returned as forward slashes.

readback to summary
public InputStream read()

Returns a stream for reading this file as bytes.

Exceptions
GdxRuntimeException:
if the file handle represents a directory, doesn't exist, or could not be read.
readback to summary
public BufferedInputStream read(int bufferSize)

Returns a buffered stream for reading this file as bytes.

Exceptions
GdxRuntimeException:
if the file handle represents a directory, doesn't exist, or could not be read.
readBytesback to summary
public byte[] readBytes()

Reads the entire file into a byte array.

Exceptions
GdxRuntimeException:
if the file handle represents a directory, doesn't exist, or could not be read.
readBytesback to summary
public int readBytes(byte[] bytes, int offset, int size)

Reads the entire file into the byte array. The byte array must be big enough to hold the file's data.

Parameters
bytes:byte[]

the array to load the file into

offset:int

the offset to start writing bytes

size:int

the number of bytes to read, see length()

Returns:int

the number of read bytes

readerback to summary
public Reader reader()

Returns a reader for reading this file as characters the platform's default charset.

Exceptions
GdxRuntimeException:
if the file handle represents a directory, doesn't exist, or could not be read.
readerback to summary
public Reader reader(String charset)

Returns a reader for reading this file as characters.

Exceptions
GdxRuntimeException:
if the file handle represents a directory, doesn't exist, or could not be read.
readerback to summary
public BufferedReader reader(int bufferSize)

Returns a buffered reader for reading this file as characters using the platform's default charset.

Exceptions
GdxRuntimeException:
if the file handle represents a directory, doesn't exist, or could not be read.
readerback to summary
public BufferedReader reader(int bufferSize, String charset)

Returns a buffered reader for reading this file as characters.

Exceptions
GdxRuntimeException:
if the file handle represents a directory, doesn't exist, or could not be read.
readStringback to summary
public String readString()

Reads the entire file into a string using the platform's default charset.

Exceptions
GdxRuntimeException:
if the file handle represents a directory, doesn't exist, or could not be read.
readStringback to summary
public String readString(String charset)

Reads the entire file into a string using the specified charset.

Parameters
charset:String

If null the default charset is used.

Exceptions
GdxRuntimeException:
if the file handle represents a directory, doesn't exist, or could not be read.
siblingback to summary
public FileHandle sibling(String name)

Returns a handle to the sibling with the specified name.

Exceptions
GdxRuntimeException:
if this file is the root.
tempDirectoryback to summary
public static FileHandle tempDirectory(String prefix)
tempFileback to summary
public static FileHandle tempFile(String prefix)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

typeback to summary
public Files.FileType type()
writeback to summary
public OutputStream write(boolean append)

Returns a stream for writing to this file. Parent directories will be created if necessary.

Parameters
append:boolean

If false, this file will be overwritten if it exists, otherwise it will be appended.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, if it is a FileType#Classpath or FileType#Internal file, or if it could not be written.
writeback to summary
public OutputStream write(boolean append, int bufferSize)

Returns a buffered stream for writing to this file. Parent directories will be created if necessary.

Parameters
append:boolean

If false, this file will be overwritten if it exists, otherwise it will be appended.

bufferSize:int

The size of the buffer.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, if it is a FileType#Classpath or FileType#Internal file, or if it could not be written.
writeback to summary
public void write(InputStream input, boolean append)

Reads the remaining bytes from the specified stream and writes them to this file. The stream is closed. Parent directories will be created if necessary.

Parameters
append:boolean

If false, this file will be overwritten if it exists, otherwise it will be appended.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, if it is a FileType#Classpath or FileType#Internal file, or if it could not be written.
writeBytesback to summary
public void writeBytes(byte[] bytes, boolean append)

Writes the specified bytes to the file. Parent directories will be created if necessary.

Parameters
append:boolean

If false, this file will be overwritten if it exists, otherwise it will be appended.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, if it is a FileType#Classpath or FileType#Internal file, or if it could not be written.
writeBytesback to summary
public void writeBytes(byte[] bytes, int offset, int length, boolean append)

Writes the specified bytes to the file. Parent directories will be created if necessary.

Parameters
append:boolean

If false, this file will be overwritten if it exists, otherwise it will be appended.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, if it is a FileType#Classpath or FileType#Internal file, or if it could not be written.
writerback to summary
public Writer writer(boolean append)

Returns a writer for writing to this file using the default charset. Parent directories will be created if necessary.

Parameters
append:boolean

If false, this file will be overwritten if it exists, otherwise it will be appended.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, if it is a FileType#Classpath or FileType#Internal file, or if it could not be written.
writerback to summary
public Writer writer(boolean append, String charset)

Returns a writer for writing to this file. Parent directories will be created if necessary.

Parameters
append:boolean

If false, this file will be overwritten if it exists, otherwise it will be appended.

charset:String

May be null to use the default charset.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, if it is a FileType#Classpath or FileType#Internal file, or if it could not be written.
writeStringback to summary
public void writeString(String string, boolean append)

Writes the specified string to the file using the default charset. Parent directories will be created if necessary.

Parameters
append:boolean

If false, this file will be overwritten if it exists, otherwise it will be appended.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, if it is a FileType#Classpath or FileType#Internal file, or if it could not be written.
writeStringback to summary
public void writeString(String string, boolean append, String charset)

Writes the specified string to the file using the specified charset. Parent directories will be created if necessary.

Parameters
append:boolean

If false, this file will be overwritten if it exists, otherwise it will be appended.

charset:String

May be null to use the default charset.

Exceptions
GdxRuntimeException:
if this file handle represents a directory, if it is a FileType#Classpath or FileType#Internal file, or if it could not be written.