How to get complete path of a directory by giving a subset of its path in Java?
E.g If I provide \abc\xyz
, is it possible to开发者_如何学C get <drivename>:\somefolder\abc\xyz
?
Have a look at
new File("\\abc\\xyz").getAbsolutePath();
Be aware, though, that this will resolve .\abc\xyz
from the current working directory. This doesn't include the package path of the class from which you're running this code.
Update:
For file-system scans, consider using
// Method listing all files matching some filter criteria
org.apache.commons.io.FileUtils.listFiles(File directory,
IOFileFilter fileFilter,
IOFileFilter dirFilter);
// A good filter for your use case
org.apache.commons.io.filefilter.WildcardFileFilter
More details:
http://commons.apache.org/io/
I haven't tried that before, but the only algorithm I can think of is either DFS(depth-first search) or BFS (Breadth-First Search) , where the tree\graph you search on is your file system, where directories are nodes in the graph (it is a tree (acyclic)) and every directories that lead to another have a directed edge (for example abc is a node in the graph that has a directed edge from itself to xyz - another node in the tree). Once you create your tree data structure, you can perform the BFS or DFS famous algorithms to find the paths.
精彩评论