How to get the path of a file in Java

In this tutorial, we are going to see how to get the path of a file in Java. The getAbsolutePath() method is part of the File class. This function returns the absolute path of the given file. If the file path is absolute, it simply returns the path of the current file.

How to get the path of a file in Java
public class Main {
    public static void main(String args[]) 
        try {
            File file = new File("test.txt"); 
            System.out.println("Relative path: " + file.getPath()); 
            System.out.println("Absolute path: " + file.getAbsolutePath()); 
        catch (Exception e) {


Relative path: file.txt
Absolute path: C:\Users\StackHowTo\Desktop\file.txt
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *