You can use the Path api:

Path p = Paths.get(yourFileNameUri);
Path folder = p.getParent();
Answer from assylias on Stack Overflow
🌐
Mkyong
mkyong.com › home › java › how to get the filepath of a file in java
How to get the filepath of a file in Java - Mkyong.com
August 24, 2020 - For symbolic links or file path contains . or .., we can use path.toRealPath() or file.getCanonicalPath() to get the real file pah. Below is the mapping of File and Path. ... For java.nio.file.Path, we can use the following APIs to get the file path of a file.
🌐
GeeksforGeeks
geeksforgeeks.org › java › file-getpath-method-in-java-with-examples
File getPath() method in Java with Examples - GeeksforGeeks
July 11, 2025 - ... // Java program to demonstrate the // use of getPath() function import java.io.*; public class solution { public static void main(String args[]) { // try catch block to handle exceptions try { // Create a file object File f = new ...
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-file-path-absolute-canonical
Java File Path, Absolute Path and Canonical Path | DigitalOcean
August 3, 2022 - This method first converts this ... the getAbsolutePath method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as “.” and “…” from the pathname, resolving symbolic links (on UNIX platforms), and converting drive ...
🌐
Medium
medium.com › @AlexanderObregon › javas-paths-get-method-explained-9586c13f2c5c
Java’s Paths.get() Method Explained | Medium
September 8, 2024 - Explore how Java's Paths.get() method simplifies cross-platform file management by converting strings or URIs into file paths with ease.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Paths.html
Paths (Java Platform SE 8 )
April 21, 2026 - The Path is obtained by invoking the getPath method of the default FileSystem.
🌐
Baeldung
baeldung.com › home › java › java io › java – path vs file
Java – Path vs File | Baeldung
April 20, 2024 - Path path = Paths.get("baeldung/tutorial.txt"); Files.delete(path); Now, the compiler requires us to handle an IOException. Moreover, a thrown exception has details about its failure that will tell you, for example, if the file does not exist. The File class in the java.io package has poor metadata support, which leads to problems across different platforms with I/O operations requiring meta-information about files.
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › java › file path in java
File Path in Java | Delft Stack
October 12, 2023 - As you can see in the output above, only the file’s name with the extension is the output. This shows that the file is present in the same folder where the Java program file is. The getAbsolutePath() method returns a string as the file’s ...
🌐
TutorialsPoint
tutorialspoint.com › get-the-name-of-the-file-and-path-in-java
Get the name of the file and path in Java
July 30, 2019 - The name of the file and the name of the path can be obtained using the methods java.io.File.getName() and java.io.File.getPath() respectively. The getName() returns the name of the file or the directory. The getPath() returns the abstract pathname in the form of a pathname string.
🌐
HappyCoders.eu
happycoders.eu › java › file-and-directory-names-file-path-paths
File and Directory Names in Java: File, Path, Paths
November 29, 2024 - Once we have the absolute directory path in a String (for example, from a configuration file), we can pass it directly to the File constructor. The following example uses a String constant for simplicity: File directory = new File("/var/log/myapp");Code language: Java (java) For this absolute directory, the File object's getters return the following values:
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › pathOps.html
Path Operations (The Java™ Tutorials > Essential Java Classes > Basic I/O)
Path inputPath = Paths.get(args[0]); // Converts the input Path // to an absolute path. // Generally, this means prepending // the current working // directory. If this example // were called like this: // java FileTest foo // the getRoot and getParent methods // would return null // on the ...
🌐
Marco Behler
marcobehler.com › guides › java-files
How To Work With Files In Java
December 9, 2020 - Note, that the stream returned by Files.walk must also be closed (e.g. with a try-with-resources statement), otherwise the JVM will keep the file handle on the directory open, which (on Windows) effectively locks it. Let’s quickly talk about the concepts of absolute, relative & canonical paths. It’s best demonstrated with some code examples: Path p = Paths.get("./src/main/java/../resources/some.properties"); System.out.println("p.isAbsolute() = " + p.isAbsolute());
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › nio › file › Path.html
Path (Java Platform SE 7 )
A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system. Path defines the getFileName, getParent, getRoot, and subpath methods to access the path components or a subsequence of its name elements.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Path.html
Path (Java Platform SE 8 )
April 21, 2026 - A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system. Path defines the getFileName, getParent, getRoot, and subpath methods to access the path components or a subsequence of its name elements.
🌐
Code2care
code2care.org › home › java › how to get file path of a file using java code?
How to get file path of a file using Java Code? | Code2care
August 5, 2024 - */ public class FilePathExample { public static void main(String[] args) { File file = new File("data.txt"); String filePath = file.getAbsolutePath(); System.out.println("File Path: " + filePath); } } Output:
🌐
GeeksforGeeks
geeksforgeeks.org › java › path-getfilename-method-in-java-with-examples
Path getFileName() method in Java with Examples - GeeksforGeeks
July 16, 2019 - Below programs illustrate getFileName() method: Program 1: ... // Java program to demonstrate // java.nio.file.Path.getFileName() method import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; public class GFG { public ...
🌐
Tutorialspoint
tutorialspoint.com › java › io › file_getpath.htm
Java - File getPath() method
The Java File getPath() method converts the abstract pathname into pathname string.