java - Get common root of String path and list of folders for that path -
i've sqlite3 database table store list of paths this:
/mnt/sdcard/folder1/a/b/file1 /mnt/sdcard/folder1/a/b/file2 /mnt/sdcard/folder1/a/b/file3 /mnt/sdcard/folder1/a/b/file4 /mnt/sdcard/folder1/a/b/file5 /mnt/sdcard/folder1/e/c/file6 /mnt/sdcard/folder2/d/file7 /mnt/sdcard/folder2/d/file8 /mnt/sdcard/file9
what want find common root of these paths , list of first level folder (unique) of common root.
for example
first run: parent root = null (it's first run) common root -> /mnt/sdcard/ list of folders - folder1 - folder2
second run (now parent root /mnt/sdcard/folder1/) common root -> /mnt/sdcard/folder1/ (same parent root) list of folders - - e
second run (now parent root /mnt/sdcard/folder1/a/) common root -> /mnt/sdcard/folder1/a/b (same parent root) list of folders -> empty (i'll files)
is there way filters db or have code?
this question made because need provide folder view of android music library store paths in song db record.
take @ http://rosettacode.org/wiki/find_common_directory_path
it's implemented in programming languages.
this java example i've tested , used purposes.
public class commonpath { public static string commonpath(string... paths){ string commonpath = ""; string[][] folders = new string[paths.length][]; for(int = 0; < paths.length; i++){ folders[i] = paths[i].split("/"); //split on file separator } for(int j = 0; j < folders[0].length; j++){ string thisfolder = folders[0][j]; //grab next folder name in first path boolean allmatched = true; //assume have matched in case there no more paths for(int = 1; < folders.length && allmatched; i++){ //look @ other paths if(folders[i].length < j){ //if there no folder here allmatched = false; //no match break; //stop looking because we've gone far can } //otherwise allmatched &= folders[i][j].equals(thisfolder); //check if matched } if(allmatched){ //if matched folder name commonpath += thisfolder + "/"; //add answer }else{//otherwise break;//stop looking } } return commonpath; } public static void main(string[] args){ string[] paths = { "/home/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members"}; system.out.println(commonpath(paths)); string[] paths2 = { "/hame/user1/tmp/coverage/test", "/home/user1/tmp/covert/operator", "/home/user1/tmp/coven/members"}; system.out.println(commonpath(paths2)); } }
Comments
Post a Comment