/*
* Created on 2004/10/07
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.util;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
/**
*
*
*/
public class ApplicationUtil {
public static void main(String[] args) {
}
public static File searchFileAroundClass(String fileName) {
ClassLoader loader = FileProperties.class.getClassLoader();
String path = FileProperties.class.getName().replaceAll("\\.", "/");
path = path + ".class";
URL url = loader.getResource(path);
String classPath=url.toString();
//System.out.println(classPath);
if (classPath.startsWith("jar:")) {
int last = classPath.lastIndexOf("!");
classPath = classPath.substring("jar:".length(), last);
}
if (classPath.startsWith("file:/")) {
String filePath=null;
try {
filePath=URLDecoder.decode(classPath
.substring("file:/".length()),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File checkFile = new File(filePath).getParentFile();
while (true) {
File tmp = new File(checkFile, fileName);
//System.out.println(tmp.getAbsolutePath());
if (tmp.exists()) {
return tmp;
}
else {
checkFile = checkFile.getParentFile();
if (checkFile == null) {
break;
}
}
}
return null;
} else {
return null;
}
}
}
|