🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the File getAbsolutePath() method in Java programming and how to use it with an example.
1. File getAbsolutePath() Method Overview
Definition:
The getAbsolutePath() method in the File class is utilized to retrieve the absolute pathname of a file or directory represented by the File object. If the abstract pathname is already absolute, it merely returns the path. If it's relative, it transforms it into an absolute pathname.
Syntax:
public String getAbsolutePath()
Parameters:
None.
Key Points:
- The method returns a String representing the absolute path of the file or directory.
- The returned value does not always refer to an existing file or directory. It just provides the absolute path based on the current or given relative path.
- It's different from the getPath() method, which returns the path as it was given (which might be relative).
- Useful for knowing the exact location of the file or directory in the system, especially if you started with a relative path.
2. File getAbsolutePath() Method Example
import java.io.File;
public class AbsolutePathExample {
public static void main(String[] args) {
// Create a file object with a relative path
File relativeFile = new File("sample.txt");
// Retrieve and print the absolute path
System.out.println("Absolute Path: " + relativeFile.getAbsolutePath());
// Create a file object with an absolute path
File absoluteFile = new File("/Users/username/documents/sample.txt");
// Retrieve and print the absolute path
System.out.println("Absolute Path: " + absoluteFile.getAbsolutePath());
}
}
Output:
Absolute Path: /path/to/current/directory/sample.txt Absolute Path: /Users/username/documents/sample.txt
Explanation:
In the demonstrated example, two File objects are created. The first, relativeFile, is based on a relative path. When invoking getAbsolutePath() on it, the method returns the complete path based on the current directory. The second, absoluteFile, is based on an absolute path. As the path is already absolute, the method just returns the given path.
The getAbsolutePath() method is beneficial when you need to understand or display the full path of a file or directory in the system.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment