Text and Binary Files
Files and directories
File class
The File class
java.io.FileOne constructor of the File class
Constructor | Description |
---|---|
File(StringPath) | Creates a File object that refers to the specified path name. |
Methods that check a File object
Method | Returns a true value if: |
---|---|
exists() | The path name exists. |
canRead() | The path name exists and a program can read from it. |
canWrite() | The path name exists and a program can write to it. |
isDirectory() | The path name exists and refers to a directory. |
isFile() | The path name exists and refers to a file. |
Methods that get information about a File object
Method | Description |
---|---|
getName() | Returns the name of the file or directory as a String object. |
getPath() | Returns the path name as represented in the constructor as a String object. |
getAbsolutePath() | Returns the absolute path name as a String object. |
length() | Returns the length of the file in bytes as a long type. |
lastModified() | Returns a long value representing the time that the file was last modified as the number of milliseconds since January 1, 1970. |
listRoots() | A static method that returns an array of File objects representing the drives available to the current system. |
listFiles() | If the object refers to a directory, this method returns an array of File objects for the files and subdirectories of this directory. |
list() | If the object refers to a directory, this method returns an array of String objects for the files and subdirectories of this directory. |
Methods that work with File objects
Method | Description |
---|---|
setReadOnly() | Makes the object read-only. If successful, this method returns a true value. |
createNewFile() | Creates a new file for the file represented by the File object if one doesn't already exist. Returns a true value if the file is created. This method throws IOException. |
mkdirs() | Creates a new directory for the directory represented by this File object including any necessary but non-existent parent directories. Returns a true value if the directory is created. |
delete() | Deletes the file or directory represented by the File object. If successful, this method returns a true value. A directory can only be deleted if it's empty. |