- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
File Objects in Java
The File object represents the actual file/directory on the disk. Here are the list of constructors to create File Object in Java −
Sr.No. | Method & Description |
---|---|
1 | File(File parent, String child)This constructor creates a new File instance from a parent abstract pathname and a
child pathname string. |
2 | File(String pathname)This constructor creates a new File instance by converting the given pathname string
into an abstract pathname. |
3 | File(String parent, String child)This constructor creates a new File instance from a parent pathname string and a child
pathname string. |
4 | File(URI uri)This constructor creates a new File instance by converting the given file: URI into an
abstract pathname. |
Assuming an object is present in the given location, the first argument to the command line will be considered as the path and the below code will be executed −
Example
import java.io.File; public class Demo{ public static void main(String[] args){ String file_name =args[0]; File my_file = new File(file_name); System.out.println("File name is :"+my_file.getName()); System.out.println("The path to the file is: "+my_file.getPath()); System.out.println("The absolute path to the file is:" +my_file.getAbsolutePath()); System.out.println("The parent directory is :"+my_file.getParent()); if(my_file.exists()){ System.out.println("Is the file readable"+my_file.canRead()); System.out.println("The size of the file in bytes is "+my_file.length()); } } }
Output
The details about the file will be displayed here.
A class named Demo contains the main function, and a string is defined, that holds the first argument passed in the command line. The details of the file are printed on the screen, this includes name of the file, file path, the absolute path of the file, and the parent directory of the file.
- Related Articles
- File Objects in C#
- File Objects in Python?
- How can we write JSON objects to a file in Java?
- Read/Write Class Objects from/to File in C++
- File Permissions in java
- Explain widening with objects in Java.
- Sort arrays of objects in Java
- Passing and Returning Objects in Java
- Are lambda expressions objects in Java?
- Copying a File in java
- Deleting a File in java
- Set file attributes in Java
- How to create Python objects based on an XML file?
- Different ways to create Objects in Java
- How are Java objects stored in memory?

Advertisements