Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the filePath from Filename using Java

Is there a easy way to get the filePath provided I know the Filename?

like image 749
user1688404 Avatar asked Nov 22 '12 09:11

user1688404


People also ask

How do I find the absolute path of a file?

The getAbsolutePath() method is a part of File class. This function returns the absolute pathname of the given file object. If the pathname of the file object is absolute then it simply returns the path of the current file object.

How do I get the filename from the file path?

To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);

Which is the correct absolute path of a file in Java?

An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.

How do you specify a file path in Java program?

The path for a file can be obtained using the method java. io. File. getPath().


1 Answers

You can use the Path api:

Path p = Paths.get(yourFileNameUri);
Path folder = p.getParent();
like image 182
assylias Avatar answered Oct 12 '22 13:10

assylias