File
class in Java is used to represent files and directories in the file system.
File
class is present in the java.io
package.
public class File implements Serializable, Comparable
{
// constructors and methods
}
File
Class:
File
class with examples:
Method | Description | Example | Output |
---|---|---|---|
createNewFile() |
Creates a new file if it does not exist. |
File file = new File("example.txt"); if(file.createNewFile()){ System.out.println("File created"); }else{ System.out.println("File already exists"); } |
File created / File already exists |
exists() |
Checks whether the file or directory exists. |
File file = new File("example.txt"); System.out.println(file.exists()); |
true / false |
delete() |
Deletes the file or directory. |
File file = new File("example.txt"); if(file.delete()){ System.out.println("File deleted"); }else{ System.out.println("Failed to delete"); } |
File deleted / Failed to delete |
getName() |
Returns the name of the file or directory. |
File file = new File("example.txt"); System.out.println(file.getName()); |
example.txt |
getAbsolutePath() |
Returns the absolute path of the file. |
File file = new File("example.txt"); System.out.println(file.getAbsolutePath()); |
C:\Users\...\example.txt |
isFile() |
Checks if it is a file. |
File file = new File("example.txt"); System.out.println(file.isFile()); |
true / false |
isDirectory() |
Checks if it is a directory. |
File dir = new File("myFolder"); System.out.println(dir.isDirectory()); |
true / false |
mkdir() |
Creates a new directory. |
File dir = new File("myFolder"); if(dir.mkdir()){ System.out.println("Directory created"); }else{ System.out.println("Failed to create directory"); } |
Directory created / Failed to create directory |
list() |
Lists all files and directories inside a directory. |
File dir = new File("myFolder"); String[] contents = dir.list(); for(String name : contents){ System.out.println(name); } |
file1.txt file2.txt subFolder |
import java.io.File;
import java.io.IOException;
public class FileExample
{
public static void main(String[] args)
{
try
{
File file = new File("example.txt");
// Create a new file
if(file.createNewFile())
{
System.out.println("File created successfully.");
}
else
{
System.out.println("File already exists.");
}
// Check if file exists
if(file.exists())
{
System.out.println("File exists at: " + file.getAbsolutePath());
}
// Delete the file
if(file.delete())
{
System.out.println("File deleted successfully.");
}
else
{
System.out.println("Failed to delete file.");
}
}
catch (IOException e)
{
System.out.println("An error occurred: " + e.getMessage());
}
}
}
File created successfully. File exists at: C:\Users\Deepak\example.txt File deleted successfully.
IOException
when using methods like createNewFile()
or accessing file paths.mkdirs()
if you want to create nested directories along with missing parent directories.\
) and Linux/Mac (/
).exists()
) before performing operations like delete or create is a good practice to avoid errors.File
class provides metadata and file management only, not file content manipulation.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.