Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
get folder name from absolute path of a file
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