Package com.badlogic.gdx.files
Class FileHandle
- java.lang.Object
-
- com.badlogic.gdx.files.FileHandle
-
- Direct Known Subclasses:
FileHandleStream
public class FileHandle extends java.lang.ObjectRepresents a file or directory on the filesystem, classpath, Android app storage, or Android assets directory. FileHandles are created via aFilesinstance. 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 apath()orfile()may not be appropriate for all types. Use the Reader or Stream methods here to hide these dependencies from your platform independent code.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.io.Filefileprotected Files.FileTypetype
-
Constructor Summary
Constructors Modifier Constructor Description protectedFileHandle()FileHandle(java.io.File file)Creates a new absolute FileHandle for theFile.protectedFileHandle(java.io.File file, Files.FileType type)FileHandle(java.lang.String fileName)Creates a new absolute FileHandle for the file name.protectedFileHandle(java.lang.String fileName, Files.FileType type)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description FileHandlechild(java.lang.String name)Returns a handle to the child with the specified name.voidcopyTo(FileHandle dest)Copies this file or directory to the specified file or directory.booleandelete()Deletes this file or empty directory and returns success.booleandeleteDirectory()Deletes this file or directory and all children, recursively.voidemptyDirectory()Deletes all children of this directory, recursively.voidemptyDirectory(boolean preserveTree)Deletes all children of this directory, recursively.booleanequals(java.lang.Object obj)booleanexists()Returns true if the file exists.java.lang.Stringextension()Returns the file extension (without the dot) or an empty string if the file name doesn't contain a dot.java.io.Filefile()Returns a java.io.File that represents this file handle.inthashCode()booleanisDirectory()Returns true if this file is a directory.longlastModified()Returns the last modified time in milliseconds for this file.longlength()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.FileHandle[]list()Returns the paths to the children of this directory.FileHandle[]list(java.io.FileFilter filter)Returns the paths to the children of this directory that satisfy the specified filter.FileHandle[]list(java.io.FilenameFilter filter)Returns the paths to the children of this directory that satisfy the specified filter.FileHandle[]list(java.lang.String suffix)Returns the paths to the children of this directory with the specified suffix.java.nio.ByteBuffermap()Attempts to memory map this file in READ_ONLY mode.java.nio.ByteBuffermap(java.nio.channels.FileChannel.MapMode mode)Attempts to memory map this file.voidmkdirs()voidmoveTo(FileHandle dest)Moves this file to the specified file, overwriting the file if it already exists.java.lang.Stringname()java.lang.StringnameWithoutExtension()FileHandleparent()java.lang.Stringpath()java.lang.StringpathWithoutExtension()java.io.InputStreamread()Returns a stream for reading this file as bytes.java.io.BufferedInputStreamread(int bufferSize)Returns a buffered stream for reading this file as bytes.byte[]readBytes()Reads the entire file into a byte array.intreadBytes(byte[] bytes, int offset, int size)Reads the entire file into the byte array.java.io.Readerreader()Returns a reader for reading this file as characters the platform's default charset.java.io.BufferedReaderreader(int bufferSize)Returns a buffered reader for reading this file as characters using the platform's default charset.java.io.BufferedReaderreader(int bufferSize, java.lang.String charset)Returns a buffered reader for reading this file as characters.java.io.Readerreader(java.lang.String charset)Returns a reader for reading this file as characters.java.lang.StringreadString()Reads the entire file into a string using the platform's default charset.java.lang.StringreadString(java.lang.String charset)Reads the entire file into a string using the specified charset.FileHandlesibling(java.lang.String name)Returns a handle to the sibling with the specified name.static FileHandletempDirectory(java.lang.String prefix)static FileHandletempFile(java.lang.String prefix)java.lang.StringtoString()Files.FileTypetype()java.io.OutputStreamwrite(boolean append)Returns a stream for writing to this file.java.io.OutputStreamwrite(boolean append, int bufferSize)Returns a buffered stream for writing to this file.voidwrite(java.io.InputStream input, boolean append)Reads the remaining bytes from the specified stream and writes them to this file.voidwriteBytes(byte[] bytes, boolean append)Writes the specified bytes to the file.voidwriteBytes(byte[] bytes, int offset, int length, boolean append)Writes the specified bytes to the file.java.io.Writerwriter(boolean append)Returns a writer for writing to this file using the default charset.java.io.Writerwriter(boolean append, java.lang.String charset)Returns a writer for writing to this file.voidwriteString(java.lang.String string, boolean append)Writes the specified string to the file using the default charset.voidwriteString(java.lang.String string, boolean append, java.lang.String charset)Writes the specified string to the file using the specified charset.
-
-
-
Field Detail
-
file
protected java.io.File file
-
type
protected Files.FileType type
-
-
Constructor Detail
-
FileHandle
protected FileHandle()
-
FileHandle
public FileHandle(java.lang.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 theFilesinterface instead.- Parameters:
fileName- the filename.
-
FileHandle
public FileHandle(java.io.File file)
Creates a new absolute FileHandle for theFile. 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 theFilesinterface instead.- Parameters:
file- the file.
-
FileHandle
protected FileHandle(java.lang.String fileName, Files.FileType type)
-
FileHandle
protected FileHandle(java.io.File file, Files.FileType type)
-
-
Method Detail
-
path
public java.lang.String path()
- Returns:
- 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.
-
name
public java.lang.String name()
- Returns:
- the name of the file, without any parent paths.
-
extension
public java.lang.String extension()
Returns the file extension (without the dot) or an empty string if the file name doesn't contain a dot.
-
nameWithoutExtension
public java.lang.String nameWithoutExtension()
- Returns:
- the name of the file, without parent paths or the extension.
-
pathWithoutExtension
public java.lang.String pathWithoutExtension()
- Returns:
- the path and filename without the extension, e.g. dir/dir2/file.png -> dir/dir2/file. backward slashes will be returned as forward slashes.
-
type
public Files.FileType type()
-
file
public java.io.File file()
Returns a java.io.File that represents this file handle. Note the returned file will only be usable forFiles.FileType.AbsoluteandFiles.FileType.Externalfile handles.
-
read
public java.io.InputStream read()
Returns a stream for reading this file as bytes.- Throws:
GdxRuntimeException- if the file handle represents a directory, doesn't exist, or could not be read.
-
read
public java.io.BufferedInputStream read(int bufferSize)
Returns a buffered stream for reading this file as bytes.- Throws:
GdxRuntimeException- if the file handle represents a directory, doesn't exist, or could not be read.
-
reader
public java.io.Reader reader()
Returns a reader for reading this file as characters the platform's default charset.- Throws:
GdxRuntimeException- if the file handle represents a directory, doesn't exist, or could not be read.
-
reader
public java.io.Reader reader(java.lang.String charset)
Returns a reader for reading this file as characters.- Throws:
GdxRuntimeException- if the file handle represents a directory, doesn't exist, or could not be read.
-
reader
public java.io.BufferedReader reader(int bufferSize)
Returns a buffered reader for reading this file as characters using the platform's default charset.- Throws:
GdxRuntimeException- if the file handle represents a directory, doesn't exist, or could not be read.
-
reader
public java.io.BufferedReader reader(int bufferSize, java.lang.String charset)Returns a buffered reader for reading this file as characters.- Throws:
GdxRuntimeException- if the file handle represents a directory, doesn't exist, or could not be read.
-
readString
public java.lang.String readString()
Reads the entire file into a string using the platform's default charset.- Throws:
GdxRuntimeException- if the file handle represents a directory, doesn't exist, or could not be read.
-
readString
public java.lang.String readString(java.lang.String charset)
Reads the entire file into a string using the specified charset.- Parameters:
charset- If null the default charset is used.- Throws:
GdxRuntimeException- if the file handle represents a directory, doesn't exist, or could not be read.
-
readBytes
public byte[] readBytes()
Reads the entire file into a byte array.- Throws:
GdxRuntimeException- if the file handle represents a directory, doesn't exist, or could not be read.
-
readBytes
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- the array to load the file intooffset- the offset to start writing bytessize- the number of bytes to read, seelength()- Returns:
- the number of read bytes
-
map
public java.nio.ByteBuffer map()
Attempts to memory map this file in READ_ONLY mode. Android files must not be compressed.- Throws:
GdxRuntimeException- if this file handle represents a directory, doesn't exist, or could not be read, or memory mapping fails, or is aFiles.FileType.Classpathfile.
-
map
public java.nio.ByteBuffer map(java.nio.channels.FileChannel.MapMode mode)
Attempts to memory map this file. Android files must not be compressed.- Throws:
GdxRuntimeException- if this file handle represents a directory, doesn't exist, or could not be read, or memory mapping fails, or is aFiles.FileType.Classpathfile.
-
write
public java.io.OutputStream write(boolean append)
Returns a stream for writing to this file. Parent directories will be created if necessary.- Parameters:
append- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
GdxRuntimeException- if this file handle represents a directory, if it is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or if it could not be written.
-
write
public java.io.OutputStream write(boolean append, int bufferSize)Returns a buffered stream for writing to this file. Parent directories will be created if necessary.- Parameters:
append- If false, this file will be overwritten if it exists, otherwise it will be appended.bufferSize- The size of the buffer.- Throws:
GdxRuntimeException- if this file handle represents a directory, if it is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or if it could not be written.
-
write
public void write(java.io.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- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
GdxRuntimeException- if this file handle represents a directory, if it is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or if it could not be written.
-
writer
public java.io.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- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
GdxRuntimeException- if this file handle represents a directory, if it is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or if it could not be written.
-
writer
public java.io.Writer writer(boolean append, java.lang.String charset)Returns a writer for writing to this file. Parent directories will be created if necessary.- Parameters:
append- If false, this file will be overwritten if it exists, otherwise it will be appended.charset- May be null to use the default charset.- Throws:
GdxRuntimeException- if this file handle represents a directory, if it is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or if it could not be written.
-
writeString
public void writeString(java.lang.String string, boolean append)Writes the specified string to the file using the default charset. Parent directories will be created if necessary.- Parameters:
append- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
GdxRuntimeException- if this file handle represents a directory, if it is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or if it could not be written.
-
writeString
public void writeString(java.lang.String string, boolean append, java.lang.String charset)Writes the specified string to the file using the specified charset. Parent directories will be created if necessary.- Parameters:
append- If false, this file will be overwritten if it exists, otherwise it will be appended.charset- May be null to use the default charset.- Throws:
GdxRuntimeException- if this file handle represents a directory, if it is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or if it could not be written.
-
writeBytes
public void writeBytes(byte[] bytes, boolean append)Writes the specified bytes to the file. Parent directories will be created if necessary.- Parameters:
append- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
GdxRuntimeException- if this file handle represents a directory, if it is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or if it could not be written.
-
writeBytes
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- If false, this file will be overwritten if it exists, otherwise it will be appended.- Throws:
GdxRuntimeException- if this file handle represents a directory, if it is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or if it could not be written.
-
list
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, anFiles.FileType.Internalhandle to a directory on the classpath will return a zero length array.- Throws:
GdxRuntimeException- if this file is anFiles.FileType.Classpathfile.
-
list
public FileHandle[] list(java.io.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, anFiles.FileType.Internalhandle to a directory on the classpath will return a zero length array.- Parameters:
filter- theFileFilterto filter files- Throws:
GdxRuntimeException- if this file is anFiles.FileType.Classpathfile.
-
list
public FileHandle[] list(java.io.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, anFiles.FileType.Internalhandle to a directory on the classpath will return a zero length array.- Parameters:
filter- theFilenameFilterto filter files- Throws:
GdxRuntimeException- if this file is anFiles.FileType.Classpathfile.
-
list
public FileHandle[] list(java.lang.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, anFiles.FileType.Internalhandle to a directory on the classpath will return a zero length array.- Throws:
GdxRuntimeException- if this file is anFiles.FileType.Classpathfile.
-
isDirectory
public boolean isDirectory()
Returns true if this file is a directory. Always returns false for classpath files. On Android, anFiles.FileType.Internalhandle to an empty directory will return false. On the desktop, anFiles.FileType.Internalhandle to a directory on the classpath will return false.
-
child
public FileHandle child(java.lang.String name)
Returns a handle to the child with the specified name.
-
sibling
public FileHandle sibling(java.lang.String name)
Returns a handle to the sibling with the specified name.- Throws:
GdxRuntimeException- if this file is the root.
-
parent
public FileHandle parent()
-
mkdirs
public void mkdirs()
- Throws:
GdxRuntimeException- if this file handle is aFiles.FileType.ClasspathorFiles.FileType.Internalfile.
-
exists
public boolean exists()
Returns true if the file exists. On Android, aFiles.FileType.ClasspathorFiles.FileType.Internalhandle to a directory will always return false. Note that this can be very slow for internal files on Android!
-
delete
public boolean delete()
Deletes this file or empty directory and returns success. Will not delete a directory that has children.- Throws:
GdxRuntimeException- if this file handle is aFiles.FileType.ClasspathorFiles.FileType.Internalfile.
-
deleteDirectory
public boolean deleteDirectory()
Deletes this file or directory and all children, recursively.- Throws:
GdxRuntimeException- if this file handle is aFiles.FileType.ClasspathorFiles.FileType.Internalfile.
-
emptyDirectory
public void emptyDirectory()
Deletes all children of this directory, recursively.- Throws:
GdxRuntimeException- if this file handle is aFiles.FileType.ClasspathorFiles.FileType.Internalfile.
-
emptyDirectory
public void emptyDirectory(boolean preserveTree)
Deletes all children of this directory, recursively. Optionally preserving the folder structure.- Throws:
GdxRuntimeException- if this file handle is aFiles.FileType.ClasspathorFiles.FileType.Internalfile.
-
copyTo
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.- Throws:
GdxRuntimeException- if the destination file handle is aFiles.FileType.ClasspathorFiles.FileType.Internalfile, or copying failed.
-
moveTo
public void moveTo(FileHandle dest)
Moves this file to the specified file, overwriting the file if it already exists.- Throws:
GdxRuntimeException- if the source or destination file handle is aFiles.FileType.ClasspathorFiles.FileType.Internalfile.
-
length
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.
-
lastModified
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 forFiles.FileType.Classpathfiles. On Android, zero is returned forFiles.FileType.Internalfiles. On the desktop, zero is returned forFiles.FileType.Internalfiles on the classpath.
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
tempFile
public static FileHandle tempFile(java.lang.String prefix)
-
tempDirectory
public static FileHandle tempDirectory(java.lang.String prefix)
-
-