Java File Class

The File class is an abstract representation of file and directory pathname. A pathname can be either absolute or relative.

The File class have several methods for working with directories and files such as creating new directories or files, deleting and renaming directories or files, listing the contents of a directory etc.

Fields

ModifierTypeFieldDescription
staticStringpathSeparatorIt is system-dependent path-separator character, represented as a string for convenience.
staticcharpathSeparatorCharIt is system-dependent path-separator character.
staticStringseparatorIt is system-dependent default name-separator character, represented as a string for convenience.
staticcharseparatorCharIt is system-dependent default name-separator character.

Constructors

ConstructorDescription
File(File parent, String child)It creates a new File instance from a parent abstract pathname and a child pathname string.
File(String pathname)It creates a new File instance by converting the given pathname string into an abstract pathname.
File(String parent, String child)It creates a new File instance from a parent pathname string and a child pathname string.
File(URI uri)It creates a new File instance by converting the given file: URI into an abstract pathname.

Useful Methods

Modifier and TypeMethodDescription
static FilecreateTempFile(String prefix, String suffix)It creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.
booleancreateNewFile()It atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.
booleancanWrite()It tests whether the application can modify the file denoted by this abstract pathname.String[]
booleancanExecute()It tests whether the application can execute the file denoted by this abstract pathname.
booleancanRead()It tests whether the application can read the file denoted by this abstract pathname.
booleanisAbsolute()It tests whether this abstract pathname is absolute.
booleanisDirectory()It tests whether the file denoted by this abstract pathname is a directory.
booleanisFile()It tests whether the file denoted by this abstract pathname is a normal file.
StringgetName()It returns the name of the file or directory denoted by this abstract pathname.
StringgetParent()It returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
PathtoPath()It returns a java.nio.file.Path object constructed from the this abstract path.
URItoURI()It constructs a file: URI that represents this abstract pathname.
File[]listFiles()It returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname
longgetFreeSpace()It returns the number of unallocated bytes in the partition named by this abstract path name.
String[]list(FilenameFilter filter)It returns an array of strings naming the files and directories in the directory denoted by this abstract pathname that satisfy the specified filter.
booleanmkdir()It creates the directory named by this abstract pathname.

Java File Example 1

Output:

New File is created!

Java File Example 2

Output:

testFile1.txt
/home/Work/Project/File/testFile1.txt
true
/home/Work/Project/File/testFile1.txt Exists? true

Java File Example 3

Output:

"info.properties"
"info.properties".rtf
.DS_Store
.localized
Alok news
apache-tomcat-9.0.0.M19
apache-tomcat-9.0.0.M19.tar
bestreturn_org.rtf
BIODATA.pages
BIODATA.pdf
BIODATA.png
struts2jars.zip
workspace

Java File Example 4

Output:

"info.properties" Can Write: true Is Hidden: false Length: 15 bytes
"info.properties".rtf Can Write: true Is Hidden: false Length: 385 bytes
.DS_Store Can Write: true Is Hidden: true Length: 36868 bytes
.localized Can Write: true Is Hidden: true Length: 0 bytes
Alok news Can Write: true Is Hidden: false Length: 850 bytes
apache-tomcat-9.0.0.M19 Can Write: true Is Hidden: false Length: 476 bytes
apache-tomcat-9.0.0.M19.tar Can Write: true Is Hidden: false Length: 13711360 bytes
bestreturn_org.rtf Can Write: true Is Hidden: false Length: 389 bytes
BIODATA.pages Can Write: true Is Hidden: false Length: 707985 bytes
BIODATA.pdf Can Write: true Is Hidden: false Length: 69681 bytes
BIODATA.png Can Write: true Is Hidden: false Length: 282125 bytes
workspace Can Write: true Is Hidden: false Length: 1972 bytes





Latest Courses