在java中获得文件的
路径在我们做上传文件操作时是不可避免的。\x0d\x0a\x0d\x0aweb 上运行 \x0d\x0a1:this.getClass().getClassLoader().getResource("/").getPath()\x0d\x0athis.getClass().getClassLoader().getResource("").getPath() 得到
的是 ClassPath的绝对URI路径。\x0d\x0a如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/\x0d\x0aSystem.getProperty("user.dir")\x0d\x0athis.getClass().getClassLoader().getResource(".").getPath() 得到的是 项目的绝对路径。\x0d\x0a如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war\x0d\x0a\x0d\x0a2:this.getClass().getResource("/").getPath()\x0d\x0athis.getClass().getResource("").getPath()得到的是当前类 文件的URI目录。\x0d\x0a如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/\x0d\x0athis.getClass().getResource(".").getPath() X 不 能运行\x0d\x0a\x0d\x0a3:Thread.currentThread().getContextClassLoader().getResource("/").getPath()\x0d\x0aThread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。\x0d\x0a如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/\x0d\x0aThread.currentThread().getContextClassLoader().getResource(".").getPath() 得到的是 项目的绝对路径。\x0d\x0a如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war\x0d\x0a\x0d\x0a在本地运行中\x0d\x0a1:this.getClass().getClassLoader().getResource("").getPath()\x0d\x0athis.getClass().getClassLoader().getResource(".").getPath() 得到的是 ClassPath的绝对URI路径。\x0d\x0a如:/D:/myProjects/hp/WebRoot/WEB-INF/classes\x0d\x0athis.getClass().getClassLoader().getResource(".").getPath() X 不 能运行\x0d\x0a2:this.getClass().getResource("").getPath()\x0d\x0athis.getClass().getResource(".").getPath()得到的是当前类 文件的URI目录。\x0d\x0a如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/\x0d\x0a/D:/myProjects/hp/WebRoot/WEB-INF/classes/得到的是 ClassPath的绝对URI路径。\x0d\x0a如:/D:/myProjects/hp/WebRoot/WEB-INF/classes可以使用以下代码来获取src目录下所有的包名,类名,方
法名 以及通过一个类名获得该类下的所有方法名。
import java.io.File
import java.lang.reflect.Method
public class LoopApp {
public static void main(String[] args) throws Exception {
String packageName = ""
File root = new File(System.getProperty("user.dir") + "\\src")
loop(root, packageName)
}
public static void loop(File folder, String packageName) throws Exception {
File[] files = folder.listFiles()
for (int fileIndex = 0fileIndex <files.lengthfileIndex++) {
File file = files[fileIndex]
if (file.isDirectory()) {
loop(file, packageName + file.getName() + ".")
} else {
listMethodNames(file.getName(), packageName)
}
}
}
public static void listMethodNames(String filename, String packageName) {
try {
String name = filename.substring(0, filename.length() - 5)
Object obj = Class.forName(packageName + name)
Method[] methods = obj.getClass().getDeclaredMethods()
System.out.println(filename)
for (int i = 0i <methods.lengthi++) {
System.out.println("\t" + methods[i].getName())
}
} catch (Exception e) {
System.out.println("exception = " + e.getLocalizedMessage())
}
}
}