File Class
In this tutorial, we are going to discuss about File class in Java. A java file object represent just name of the file/directory. In Java, the File
class, found in the java.io
package, represents a file or directory path in the file system. It provides methods for file manipulation, such as checking existence, creating, deleting, and listing files and directories.
File file = new File(ashok.txt”);
If ashok.txt is already available then ‘file’ will represent that physical file.
E.g
package com.ashok.files;
import java.io.File;
import java.io.IOException;
/**
*
* @author ashok.mariyala
*
*/
public class MyFile {
public static void main(String[] args) {
File f = new File("ashok.txt");
System.out.println(f.exists());
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(f.exists());
}
}
Output
1st Run
false
true
2nd Run
true
true
A java file Object can represent directories also
package com.ashok.files;
import java.io.File;
import java.io.IOException;
/**
*
* @author ashok.mariyala
*
*/
public class MyFile {
public static void main(String[] args) {
File f = new File("ashok");
System.out.println(f.exists());
f.mkdir();
System.out.println(f.exists());
}
}
Output
1st Run
false
true
2nd Run
true
true
Constructors
1. File f = new File(String name)
Here name may be file or directory name. Creates a java file object that represents a file or directory name.
2. File f = new File(String subdirec, String name)
Creates a java file object that represents file or directory name present in specified subdirectory.
3. File f = new File(File subdir, String name)
Important methods
1. boolean exists()
Returns true if the physical file/directory presents other wise false.
2. boolean createNewFile()
Returns ture if it creates a new file, if the required file is already available then it won’t create any new file and returns false.
3. booelan mkdir()
For creation of directory.
4. boolean isFile()
Returns true if the java file object represents a file.
5. boolean isDirectory()
Returns true if the java file object represents a directory.
6. String [] list()
Returns the names of files and directories present in the directories represented by the file object. If the java file object represents a file instead of directory this method returns null.
7. Boolean delete()
For deleting the file or directory represented by java file object.
Write code to create a file named with ashok.txt in current working directory.
import java.io.File;
import java.io.IOException;
/**
*
* @author ashok.mariyala
*
*/
class MyFile {
public static void main(String[] args)throws IOException {
File f = new File("demo.txt");
f.createNewFile();
}
}
Write code to create a directory named with Waytoeasylearn in current working directory and create a file named with ashok.txt in that directory.
import java.io.File;
import java.io.IOException;
/**
*
* @author ashok.mariyala
*
*/
class MyFile {
public static void main(String[] args)throws IOException {
File f1 = new File("Waytoeasylearn");
f1.mkdir();
File f2 = new File("Waytoeasylearn","ashok.txt");
f2.createNewFile();
}
}
Write code to create a file named with ashok.txt present in c:\Waytoeasylearn folder.
import java.io.File;
import java.io.IOException;
/**
*
* @author ashok.mariyala
*
*/
class MyFile {
public static void main(String[] args)throws IOException {
File f = new File("c:\\Waytoeasylearn","ashok.txt");
f.createNewFile();
}
}
That’s all about File class in java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!