Re: Get root directory of application
Dave wrote:
How do I access the root directory of an application?
Say I want to access a file which is stored in the highest-level directory
of the application. How do I access this with a relative path rather than an
absolute path?
As several has pointed out then you need to define carefully what
is the root of an application.
Regarding finding the path to the code being executed then I have
the code snippet attached below.
Arne
=============================================
private String getPath(Class cls) {
String cn = cls.getName();
StringTokenizer st = new StringTokenizer(cn,".");
StringBuffer sb = new StringBuffer("");
boolean first = true;
while(st.hasMoreTokens()) {
if(first) {
first = false;
} else {
sb.append("/");
}
sb.append(st.nextToken());
}
sb.append(".class");
String path =
getClass().getClassLoader().getResource(sb.toString()).getPath();
int ix = path.indexOf("!");
if(ix >= 0) {
path = path.substring(0, ix);
int ix2 = path.lastIndexOf("/");
return path.substring(6, ix2 + 1);
} else {
int ix2 = path.lastIndexOf("/");
return path.substring(1, ix2 + 1);
}
}