Java File Class Methods
File Class Methods
The File
class (from java.io
) is used to work with file and directory paths.
It lets you create, delete, and get information about files and folders - but it does not read or write the contents.
Method | Description | Return Type |
---|---|---|
createNewFile() | Creates a new, empty file if it does not already exist | boolean |
delete() | Deletes the file or directory | boolean |
exists() | Tests whether the file or directory exists | boolean |
canRead() | Tests whether the application can read the file | boolean |
canWrite() | Tests whether the application can write to the file | boolean |
isFile() | Returns true if the path represents a file | boolean |
isDirectory() | Returns true if the path represents a directory | boolean |
length() | Returns the size of the file in bytes | long |
getName() | Returns the name of the file or directory | String |
getAbsolutePath() | Returns the absolute pathname string | String |
getPath() | Returns the pathname string | String |
getParent() | Returns the pathname string of the parent directory | String |
list() | Returns an array of names of files and directories in a directory | String[] |
listFiles() | Returns an array of File objects in a directory |
File[] |
mkdir() | Creates a directory | boolean |
mkdirs() | Creates a directory and any missing parent directories | boolean |
renameTo(File dest) | Renames the file or directory | boolean |
setReadOnly() | Marks the file as read-only | boolean |
setWritable(boolean writable) | Sets the writable permission | boolean |
setReadable(boolean readable) | Sets the readable permission | boolean |
setExecutable(boolean executable) | Sets the executable permission | boolean |
canExecute() | Tests whether the application can execute the file | boolean |