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
Modifie Type Field Description
r
static Strin pathSeparator It is system-dependent path-separator character, repr
g a string for convenience.
static char pathSeparatorCha It is system-dependent path-separator character.
r
static Strin separator It is system-dependent default name-separator charac
g a string for convenience.
static char separatorChar It is system-dependent default name-separator charac
Constructors
Constructor Description
File(File parent, String child) It creates a new File instance from a parent abstract pathname and a
string.
File(String pathname) It creates a new File instance by converting the given pathname strin
pathname.
File(String parent, String It creates a new File instance from a parent pathname string and a c
child) string.
File(URI uri) It creates a new File instance by converting the given file: URI into a
pathname.
Useful Methods
Modifier Method Description
and Type
static File createTempFile(String prefix, It creates an empty file in the default temporary-
String suffix) the given prefix and suffix to generate its name.
boolean createNewFile() It atomically creates a new, empty file named by
pathname if and only if a file with this name does
boolean canWrite() It tests whether the application can modify the fil
abstract pathname.String[]
boolean canExecute() It tests whether the application can execute the f
abstract pathname.
boolean canRead() It tests whether the application can read the file d
abstract pathname.
boolean isAbsolute() It tests whether this abstract pathname is absolut
boolean isDirectory() It tests whether the file denoted by this abstract p
directory.
boolean isFile() It tests whether the file denoted by this abstract p
normal file.
String getName() It returns the name of the file or directory denote
pathname.
String getParent() It returns the pathname string of this abstract pa
null if this pathname does not name a parent dire
Path toPath() It returns a java.nio.file.Path object constructed f
abstract path.
URI toURI() It constructs a file: URI that represents this abstr
File[] listFiles() It returns an array of abstract pathnames denotin
directory denoted by this abstract pathname
long getFreeSpace() It returns the number of unallocated bytes in the
this abstract path name.
String[] list(FilenameFilter filter) It returns an array of strings naming the files and
directory denoted by this abstract pathname that
filter.
boolean mkdir() It creates the directory named by this abstract pa
Java File Example 1
1. import java.io.*;
2. public class FileDemo {
3. public static void main(String[] args) {
4.
5. try {
6. File file = new File("javaFile123.txt");
7. if (file.createNewFile()) {
8. System.out.println("New File is created!");
9. } else {
10. System.out.println("File already exists.");
11. }
12. } catch (IOException e) {
13. e.printStackTrace();
14. }
15.
16. }
17. }
Output:
New File is created!
Java File Example 2
1. import java.io.*;
2. public class FileDemo2 {
3. public static void main(String[] args) {
4.
5. String path = "";
6. boolean bool = false;
7. try {
8. // createing new files
9. File file = new File("testFile1.txt");
10. file.createNewFile();
11. System.out.println(file);
12. // createing new canonical from file object
13. File file2 = file.getCanonicalFile();
14. // returns true if the file exists
15. System.out.println(file2);
16. bool = file2.exists();
17. // returns absolute pathname
18. path = file2.getAbsolutePath();
19. System.out.println(bool);
20. // if file exists
21. if (bool) {
22. // prints
23. System.out.print(path + " Exists? " + bool);
24. }
25. } catch (Exception e) {
26. // if any error occurs
27. e.printStackTrace();
28. }
29. }
30. }
Output:
testFile1.txt
/home/Work/Project/File/testFile1.txt
true
/home/Work/Project/File/testFile1.txt Exists? true
Java File Example 3
1. import java.io.*;
2. public class FileExample {
3. public static void main(String[] args) {
4. File f=new File("/Users/sonoojaiswal/Documents");
5. String filenames[]=f.list();
6. for(String filename:filenames){
7. System.out.println(filename);
8. }
9. }
10. }
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
1. import java.io.*;
2. public class FileExample {
3. public static void main(String[] args) {
4. File dir=new File("/Users/sonoojaiswal/Documents");
5. File files[]=dir.listFiles();
6. for(File file:files){
7. System.out.println(file.getName()+" Can Write: "+file.canWrite()+"
8. Is Hidden: "+file.isHidden()+" Length: "+file.length()+" bytes");
9. }
10. }
11. }
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