Created
November 18, 2015 00:18
-
-
Save cofearabi/68d2648e6c3f4634938e to your computer and use it in GitHub Desktop.
get folder name from absolute path of a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.util.Stack; | |
import java.util.StringTokenizer; | |
public class Sample { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
// System.out.println(splitbyslash("/hello/world/today/sample.mdb")); | |
System.out.println(get_folder_name("/home/komo2/Dropbox/doc/mdb2/sample.mdb")); | |
} | |
public static int splitbyslash(String line){ | |
Stack<String> mDirectorys = new Stack<String>(); | |
StringTokenizer token = new StringTokenizer(line,"/"); | |
String str_tmp = "/"; | |
mDirectorys.push(str_tmp); | |
System.out.println(str_tmp); | |
while(token.hasMoreTokens()){ | |
str_tmp = str_tmp + token.nextToken() + "/"; | |
mDirectorys.push(str_tmp); | |
System.out.println(str_tmp); | |
} | |
System.out.println("-------"); | |
mDirectorys.pop(); | |
System.out.println(mDirectorys.pop()); | |
System.out.println("-------"); | |
while(mDirectorys.size()>0){ | |
System.out.println(mDirectorys.pop()); | |
} | |
return line.length(); | |
} | |
public static String get_folder_name(String line){ | |
Stack<String> mDirectorys = new Stack<String>(); | |
StringTokenizer token = new StringTokenizer(line,"/"); | |
String str_tmp = "/"; | |
mDirectorys.push(str_tmp); | |
while(token.hasMoreTokens()){ | |
str_tmp = str_tmp + token.nextToken() + "/"; | |
mDirectorys.push(str_tmp); | |
} | |
String folder_name; | |
while(true){ | |
folder_name =mDirectorys.pop(); | |
File folder0 = new File(folder_name); | |
if(folder0.isDirectory()){ | |
return folder_name; | |
} | |
folder0=null; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment