Java Program to Get File Name from Absolute Path

Java Program to Get File Name from Absolute Path

What is Absolute Path ?

An absolute path in Java is a file or directory path that details a file or directory’s whole location within a file system, starting from the root directory.
Depending on the operating system, Java has a different syntax for specifying an absolute path. In a Unix-based system, the absolute path begins with a forward slash (e.g., “/home/john/documents/file.txt”), whereas in a Windows operating system, the absolute path might start with a drive letter followed by a colon (e.g., “C:UsersJohnDocumentsfile.txt”).

Ways to Get File Name from Absolute Path in Java : 

File file = new File("C:/Users/John/Documents/file.txt");
String fileName = file.getName();
System.out.println(fileName); // Output: file.txt
Path path = Paths.get("C:/Users/John/Documents/file.txt");
String fileName = path.getFileName().toString();
System.out.println(fileName); // Output: file.txt
String path = "C:/Users/John/Documents/file.txt";
String fileName = path.substring(path.lastIndexOf("/") + 1);
System.out.println(fileName); // Output: file.txt

Example 1 : 

Run

import java.io.File;

public class Main {
    public static void main(String[] args) {
        // Absolute path of the file
        String absolutePath = "C:/Users/John/Documents/file.txt";

        // Create a File object with the absolute path
        File file = new File(absolutePath);

        // Get the file name using the getName() method
        String fileName = file.getName();

        // Print the file name
        System.out.println("File name: " + fileName);
    }
}

Output :

File name: file.tx

Example 2 : 

Run
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    public static void main(String[] args) {
        // Absolute path of the file
        String absolutePath = "C:/Users/John/Documents/file.txt";

        // Create a Path object with the absolute path
        Path path = Paths.get(absolutePath);

        // Get the file name using the getFileName() method
        String fileName = path.getFileName().toString();

        // Print the file name
        System.out.println("File name: " + fileName);
    }
}

Output :

File name: file.txt

Example 3 : 

Run
public class Main {
    public static void main(String[] args) {
        // Absolute path of the file
        String absolutePath = "C:/Users/John/Documents/file.txt";

        // Extract the file name using string manipulation
        String fileName = absolutePath.substring(absolutePath.lastIndexOf("/") + 1);

        // Print the file name
        System.out.println("File name: " + fileName);
    }
}

Output :

File name: file.txt

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription