java路径

import java.io.File; /** * @description <p>java 路径</p> * @author swandragon * @date Nov 5, 2009 */ public class FilePath{ /** * 开发中不要使用System.getProperty("user.dir")获取当前用户目录的相对路径 * 也尽可能不要使用绝对路径,使用绝对路径时可以在配置文件中配置,项目中读取配置文件 * 应尽可能的使用相对路径,推荐使用当前类的classpath的URI目录路径 * 使用相对于classpath的路径而不是相对于源文件的路径,因为项目运行的是类文件 */ public static void main(String[] args){ //用户当前工作目录,绝对路径 System.out.println(System.getProperty("user.dir")); //java类路径 ,包括路径中的jar包 System.out.println(System.getProperty("java.class.path")); //加载库时搜索路径(jdk bin目录,当前目录,系统目录/WINDOWS;/WINDOWS/system32,环境变量path目录) System.out.println(System.getProperty("java.library.path")); //用户当前目录 (项目跟目录绝对路径) File file = new File(""); System.out.println(file.getAbsolutePath()); //用户当前目录 (项目跟目录绝对路径) file = new File("."); System.out.println(file.getAbsolutePath()); //用户当前目录 (项目跟目录绝对路径) file = new File(".//"); System.out.println(file.getAbsolutePath()); //用户当前目录的上级目录 (项目跟目录绝对路径的上级目录) file = new File(".."); System.out.println(file.getAbsolutePath()); //用户当前目录的上级目录 (项目跟目录绝对路径的上级目录) //(..//images) images与当前目录的上级目录为同级目录 file = new File("..//"); System.out.println(file.getAbsolutePath()); //当前类FilePath的类文件的URI目录 包括FilePath类所在的包 System.out.println(FilePath.class.getResource("")); //当前类的classpath的URI目录 System.out.println(FilePath.class.getResource("/")); //当前类的classpath的URI目录 System.out.println(FilePath.class.getClassLoader().getResource("")); //当前类的classpath的URI目录 System.out.println(Thread.currentThread().getContextClassLoader().getResource("")); //当前类的classpath的URI目录 System.out.println(ClassLoader.getSystemResource("")); } }

 

你可能感兴趣的:(java路径)