public class File
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
CACHE_ROOT
A root folder for cache.
|
static java.lang.String |
DOCUMENTS_ROOT
A root folder for documents.
|
static java.lang.String |
EXTERNAL_STORAGE_ROOT
A root folder for external storage.
|
static java.lang.String |
RESOURCES_ROOT
A root folder for resources.
|
Constructor and Description |
---|
File(File parent,
java.lang.String child)
Creates a new File instance based on its parent and path.
|
File(java.lang.String pathname)
Creates a new File instance based on its path.
|
File(java.lang.String parent,
java.lang.String child)
Creates a new File instance based on its parent and path.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canExecute()
Checks if the file is marked as executable.
|
boolean |
canRead()
Checks if the file can be read from.
|
boolean |
canWrite()
Checks if the file can be written to.
|
boolean |
copy(File destFile,
boolean replaceExisting)
Copies the current file or directory into the destination one.
|
boolean |
createNewFile()
Create a new zero-length file at the current path.
|
boolean |
delete()
Deletes the file.
|
boolean |
deleteRecursive()
Deletes directory and its content recursively.
|
boolean |
equals(java.lang.Object obj) |
boolean |
exists()
Checks if the file exists.
|
File |
getAbsoluteFile()
Returns the absolute file.
|
java.lang.String |
getAbsolutePath()
Returns the absolute pathname for this file.
|
static java.lang.String |
getCacheRoot()
Returns a root folder for caches.
|
File |
getCanonicalFile()
Returns the canonical pathname of the current file.
|
java.lang.String |
getCanonicalPath()
Returns the canonical representation of the current file.
|
static java.lang.String |
getDocumentsRoot()
Returns a root folder for documents.
|
static java.lang.String |
getExternalStorageRoot()
Returns a root folder for external storage.
|
java.lang.String |
getName()
Returns the name of the file or directory.
|
java.lang.String |
getName(int idx)
Returns the name at the given index.
|
int |
getNameCount()
Returns the number of file and directory names in the path.
|
java.lang.String[] |
getNames()
Returns an array of names.
|
java.lang.String |
getParent()
Returns the name of parent directory.
|
File |
getParentFile()
Returns a new File instance initialized with the String result of
this.getParent() . |
java.lang.String |
getPath()
Returns the full path of the file or directory as it was initialized.
|
static java.lang.String |
getResourcesRoot()
Returns a root folder for the project's resources.
|
int |
hashCode() |
boolean |
isAbsolute()
Checks if the path is absolute or relative.
|
boolean |
isDirectory()
Checks if the current path points to an existing directory.
|
boolean |
isFile()
Checks if the current path points to an existing file.
|
long |
lastModified()
Returns the last date when the file or directory was modified.
|
long |
length()
Returns the length of the file in bytes.
|
java.lang.String[] |
list()
Returns an array of names of directories and files within this directory.
|
java.lang.String[] |
list(FilenameFilter filter)
Returns an array of only names of directories and files within this directory which are accepted by the
filter |
File[] |
listFiles()
Returns an array of directories and files within the current directory.
|
File[] |
listFiles(FileFilter filter)
Returns an array of only directories and files within the current
directory which are accepted by the
filter |
static File[] |
listRoots()
Returns an array of root folders.
|
boolean |
mkdir()
Create a new folder at the current path if the parent folder exists.
|
boolean |
mkdirs()
Creates a new folder at the current path, along with any
parent folders that do not currently exist.
|
byte[] |
read()
Reads the entire file at the current path.
|
boolean |
renameTo(File dest)
Renames the current file or directory to the destination one.
|
File |
subfile(int beginIndex,
int endIndex)
Returns a new file instance initialized with the sub-path.
|
java.lang.String |
toString()
Returns the string representation of this file.
|
void |
write(byte[] bytes,
boolean append)
Writes an array of bytes to the file at the current path.
|
public static final java.lang.String DOCUMENTS_ROOT
public static final java.lang.String CACHE_ROOT
public static final java.lang.String RESOURCES_ROOT
public static final java.lang.String EXTERNAL_STORAGE_ROOT
public File(java.lang.String pathname)
pathname
- - path to the file.java.lang.IllegalArgumentException
- if pathname contains invalid characters.public File(File parent, java.lang.String child)
child
- - path to the file.java.lang.IllegalArgumentException
- if child contains invalid characters.public File(java.lang.String parent, java.lang.String child)
child
- - path to the file.java.lang.IllegalArgumentException
- if child or parent contain invalid characters.public static java.lang.String getDocumentsRoot()
DOCUMENTS_ROOT
.public static java.lang.String getCacheRoot()
CACHE_ROOT
.public static java.lang.String getResourcesRoot()
RESOURCES_ROOT
.public static java.lang.String getExternalStorageRoot()
EXTERNAL_STORAGE_ROOT
.public boolean canExecute()
public boolean canRead()
public boolean canWrite()
public boolean delete()
public boolean deleteRecursive()
public boolean exists()
public File getAbsoluteFile()
new File(this.getAbsolutePath())
.public java.lang.String getAbsolutePath()
DOCUMENTS_ROOT
will be used as root: "/documents/a.txt".public java.lang.String getCanonicalPath()
new File(this.getCanonicalPath())
public File getCanonicalFile()
"a//.././b/" -> "/documents/b" ".." -> "/documents" "/a/b/../c" -> "/a/b"
public java.lang.String getName()
public java.lang.String getParent()
"a/b" -> "a" "/a" -> "/" "a" -> null
public File getParentFile()
this.getParent()
.null
, if this.getParent()
returns null
.public java.lang.String getPath()
public boolean isAbsolute()
public boolean isDirectory()
public boolean isFile()
public long lastModified()
public long length()
public java.lang.String[] list()
null
if the current path points to a non-existing directory.public java.lang.String[] list(FilenameFilter filter)
filter
filter
- - the file filter.null
if the current path points to a non-existing directory.public File[] listFiles()
null
if the current path points to a non-existing directory.public File[] listFiles(FileFilter filter)
filter
null
if the
current path points to a non-existing directory.public static File[] listRoots()
"/documents" "/cache" "/external_storage" "/resources"
public boolean mkdir()
public boolean mkdirs()
public boolean renameTo(File dest)
dest
- - the destination file or directory.public byte[] read() throws java.io.IOException
java.io.IOException
- if file cannot be read.public void write(byte[] bytes, boolean append) throws java.io.IOException
bytes
- - the data to be written to the file.append
- - if true, the data will be appended to the existing file,
otherwise the current file will be overwritten.java.io.IOException
- if the data cannot be written.public boolean createNewFile() throws java.io.IOException
java.io.IOException
- if the file cannot be created for some reason.public boolean copy(File destFile, boolean replaceExisting) throws java.io.IOException
destFile
- - the destination file or directory.replaceExisting
- - if true, the existing file or directory will be replaced,
otherwise the file in the destination folder will be kept.java.io.IOException
- if file or directory cannot be coppied.public int getNameCount()
"" -> 1 "/" -> 1 "../.." -> 2 "a/b/c" -> 3 "/a/../../" -> 3
public java.lang.String getName(int idx)
idx
- - indexjava.lang.IndexOutOfBoundsException
- if idx
is out of range.public java.lang.String[] getNames()
"/a/.." -> ["a", ".."] "a/b/c/" -> ["a", "b", "c"]
public File subfile(int beginIndex, int endIndex)
new File("/a/b/c").subfile(0, 1) -> new File("a")
new File("/a/b/c").subfile(1, 3) -> new File("b/c")
new File("/a/b/c").subfile(2, 3) -> new File("c")
beginIndex
- - the start index of the path. Must be less than endIndex
endIndex
- - the end index of the path. Must be less than or equal to this.getNameCount()
java.lang.IllegalArgumentException
- if beginIndex
or endIndex
are our of range.public java.lang.String toString()
toString
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object