java——文件的路径

public class FileTest {
public static void main(String[] args) throws Exception {

/**
* currentThread()返回对当前正在执行的线程的引用 
* getContextClassLoader()返回该线程的上下文 ClassLoader
* getResource()查找具有给定名称的资源
*/
System.out.println(Thread.currentThread().getContextClassLoader()
.getResource(""));
/**
* getClassLoader返回该类的类加载器(ClassLoader)。 
* ClassLoader.getResource()查找具有给定名称的资源
//    */
System.out.println(FileTest.class.getClassLoader().getResource("db_config.properties"));

/*
* ClassLoader类加载器是负责加载类的对象。 一般策略是将名称转换为某个文件名,然后从文件系统读取该名称的“类文件”。
* getSystemResource()从用来加载类的搜索路径中查找具有指定名称的资源。
*/
System.out.println(ClassLoader
.getSystemResource("db_config.properties"));

//查找带有给定名称的资源。查找与给定类相关的资源的规则是通过定义类的 class loader 实现的。
System.out.println(FileTest.class.getResource(""));

/*
* Class.getResource() 查找带有给定名称的资源。查找与给定类相关的资源的规则是通过定义类的 class loader
* 实现的。 此方法委托给此对象的类加载器。 如果此对象通过引导类加载器加载,则此方法将委托给
* ClassLoader.getSystemResource(java.lang.String)。 返回:url
*/
System.out.println(FileTest.class.getResource("/").toString()
.replace("file:/", "")); // Class文件所在路径
//getAbsolutePath()返回此抽象路径名的绝对路径名字符串。
System.out.println(new File("").getAbsolutePath());

System.out.println(System.getProperty("user.dir"));
}
}
原文地址:https://www.cnblogs.com/rsdqc/p/5479256.html