How to Know Where the Actual File is Getting Stored in Java?


In Java, you can use the File class to represent and manipulate file and directory paths. You can also use the File class to create, delete, and manipulate files and directories.

To know where the actual file is getting stored in Java, you can use the getAbsolutePath() method of the File class. This method returns a string representation of the absolute path of the file, which includes the full path from the root directory to the file.

Let's deep dive into the article to see where the actual file is getting saved in Java.

For instance

Suppose the source file be:

" example.txt”

After performing the operation to get where the actual file is getting store, the result will be:

Absolute path of file: C:\Users\SAMPLE \Desktop\satya\Program\example.txt

Algorithm

Step-1: Declare and initialize the source file path.

Step-2: Get the location of the actual file using getAbsolutePath() method.

Step-3: Print the result.

Syntax

getAbsolutePath(): It is a method in the File class of the Java API that returns the absolute path of a file or directory as a String.

Multiple Approaches

We have provided the solution in different approaches.

1. By Using Static Method

2. By Using User Defined Method

Let's see the program along with its output one by one.

Approach-1: By Using Static Method

In this approach initialize a File class object by passing the file name. Then as per the algorithm by using getAbsolutePath() method get the absolute path where the file is getting stored in java.

Example

import java.io.File;

public class Main 
{
   //main method
   public static void main(String[] args) 
   {
      File file = new File("example.txt");
	  //getting the location of actual file
      String absolutePath = file.getAbsolutePath();
	  //print the result
      System.out.println("Absolute path of file: " + absolutePath);
   }
}

Output

Absolute path of file: /home/cg/root/64927fe34456a/example.txt

Approach-2: By Using User Defined Method

In this approach from the main method call a user defined method. In the user defined method initialize a File class object by passing the file name. Then as per the algorithm by using getAbsolutePath() method get the absolute path where the file is getting stored in java.

Example

import java.io.File;

public class Main
{
   //main method
   public static void main(String[] args) 
   {
    //calling user defined method
    func();
   }

   //user defined method
   static void func()
   {
	  //getting the file path 
      File file = new File("myfile.txt");

	  //getting the location of actual file
      String absolutePath = file.getAbsolutePath();

	  //print the result
      System.out.println("Absolute path of file: " + absolutePath);
   }
}

Output

Absolute path of file: /home/cg/root/64927fe34456a/myfile.txt

In this article, we explored how to know where the actual file is getting stored by using Java programming language.

Updated on: 17-Aug-2023

48 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements