
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get File Object with Absolute Path for Directory or File in Java
The method java.io.File.getAbsoluteFile() is used to get the File object with the absolute path for the directory or file. This method requires no parameters.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java"); System.out.println("The absolute file is: " + file.getAbsoluteFile()); } }
The output of the above program is as follows −
Output
The absolute file is:/C:/jdk11.0.2/demo1.java
Now let us understand the above program.
The file object with the absolute path is obtained using the method java.io.File.getAbsolutePath() in the form of a string and is printed. A code snippet that demonstrates this is given as follows −
File file = new File("C:" + File.separator + "jdk11.0.2" + File.separator, "demo1.java"); System.out.println("The absolute file is: " + file.getAbsoluteFile());
Advertisements