Retrieving the path of a file in Java

The File.getAbsolutePath() method returns the name of the file's full path (path + name).

Example:

File file = File("C:\\Dossier_111\\fichier.txt"); 
System.out.println("Path: " + file.getAbsolutePath());
It will display the full path: "C:\\Dossier_111\\fichier.txt".

In most cases, you will need to retrieve only the path without the file name using functions like substring() and lastIndexOf().

File file = File("C:\\ File111\\fichier.txt"); 
String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.
substring(0,absolutePath.lastIndexOf(File.separator));
System.out.println("Path: " + filePath);
Runtime:

C:\\Folder111

Resources:
tutorialspoint - Java.io.File.getAbsolutePath() Method