java 路径问题

classpath概念:

类路径:也就是可执行文件.class文件所在路径
java项目中:%项目%/bin下
javaweb项目中:%项目%/WEB-INF/classes下

Class.getResource(""):
Class.getResource("/"):

java项目中:

    public static void main(String[] args) {
        URL url = Test.class.getResource("");
        System.out.println("路径:"+url);  //在classpath/类所在包下
        
        url = Test.class.getResource("/");
        System.out.println("路径/:"+url);  //在classpath下
        
    }
路径:file:/D:/workspace/tmp/bin/com/xxjqr/tmp/test/
路径/:file:/D:/workspace/tmp/bin/

web项目中:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        URL url = DownloadServlet.class.getResource("");
        System.out.println("路径:"+url);  //在classpath/类所在包下
        
        url = DownloadServlet.class.getResource("/");
        System.out.println("路径/:"+url);  //在classpath下
        
    }
路径:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/com/xxjqr/jdbc_study/web/
路径/:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/

Class.getClassLoader().getResource(""):
Class.getClassLoader().getResource("/"):

java项目中

    public static void main(String[] args) {
        URL url = Test.class.getClassLoader().getResource("");
        System.out.println("路径:"+url);  //在classpath/类所在包下
        url = Test.class.getClassLoader().getResource("/");
        System.out.println("路径/:"+url);  //null
    }
路径:file:/D:/workspace/tmp/bin/
路径/:null

web项目中

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        URL url = DownloadServlet.class.getClassLoader().getResource("");
        System.out.println("路径:"+url);  //classpath下
        url = DownloadServlet.class.getClassLoader().getResource("/");
        System.out.println("路径/:"+url);  //classpath下

    }
路径:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/
路径/:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/

Thread.currentThread().getContextClassLoader().getResource("");
Thread.currentThread().getContextClassLoader().getResource("/");

web项目中

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        URL url = Thread.currentThread().getContextClassLoader().getResource("");
        System.out.println("路径:"+url);  //classpath下
        
        url = Thread.currentThread().getContextClassLoader().getResource("");
        System.out.println("路径/:"+url);  //classpath下
    }
路径:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/
路径/:file:/D:/tomcat/apache-tomcat-7.0.70/webapps/jdbc_study/WEB-INF/classes/

getServletContext().getRealPath(""):
getServletContext().getRealPath("/"):

web项目中

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String path = getServletContext().getRealPath("");
        System.out.println("路径:"+path);  //项目根目录下
        
        path = getServletContext().getRealPath("/");
        System.out.println("路径/:"+path);  //项目根目录下
    }
路径:D:\tomcat\apache-tomcat-7.0.70\webapps\jdbc_study
路径/:D:\tomcat\apache-tomcat-7.0.70\webapps\jdbc_study\

getServletContext().getResource(""):
getServletContext().getResource("/"):

web项目中

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        URL url = getServletContext().getResource("");
        System.out.println("路径:"+url);  //项目根目录下
        
        url = getServletContext().getResource("/");
        System.out.println("路径/:"+url);  //项目根目录下
    }
路径:jndi:/localhost/jdbc_study/
路径/:jndi:/localhost/jdbc_study/
补充:

.:是相对路径,表示当前项目,只能在java项目中使用

获取资源举例:

java项目中获取资源文件:

java 路径问题_第1张图片
Paste_Image.png
java 路径问题_第2张图片
Paste_Image.png

java项目中scr下的资源文件分别在src,bin目录下都有一份,所以可以通过如下方法来获取

public class Test2 {
    public static void main(String[] args) throws IOException {
        //--------获取src下文件-----
        //方式1:
//        InputStream in = new FileInputStream(new File("./bin/user.properties"));    
//        InputStream in = new FileInputStream(new File("./src/user.properties"));
        
        //方式2:
//        Class clazz = Test2.class;
//        InputStream in= clazz.getResourceAsStream("/user.properties");//bin目录下的资源文件

        //方式3:
//        InputStream in = Test2.class.getClassLoader().getResourceAsStream("user.properties");
        
//        Properties properties = new Properties();
//        properties.load(in);
//        String name = properties.getProperty("name");
//        String password = properties.getProperty("password");
//        System.out.println(name+":"+password);
        
        
      //--------获取file目录下文件-----
        //方式1:
//        InputStream in = new FileInputStream(new File("./src/file/乔布斯.txt"));
        
        //方式2:
//      Class clazz = Test2.class;
//      InputStream in= clazz.getResourceAsStream("/file/乔布斯.txt");//bin目录下的资源文件

        //方式3:
        InputStream in = Test2.class.getClassLoader().getResourceAsStream("file/乔布斯.txt");
        byte buffer[] = new byte[1024];
        int length = 0;
        while((length = in.read(buffer))!=-1){
            System.out.println(new String(buffer,0,length));
        }
    }
}

web项目中获取资源文件:

java 路径问题_第3张图片
Paste_Image.png
java 路径问题_第4张图片
Paste_Image.png
java 路径问题_第5张图片
Paste_Image.png

查看项目的配置就可以知道web项目中src下的内容会被组装到WEB/INF/classes下;而classes又正是web项目的执行类根目录;所以可以通过如下方法来获取

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //----------读取src下文件---------
        //方式1:
//      InputStream in = DownloadServlet.class.getResourceAsStream("/user.properties");
        
        //方式2:
//      InputStream in = DownloadServlet.class.getClassLoader().getResourceAsStream("user.properties");
        
        //方式3:
//      InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("user.properties");

        //方式4:
//      InputStream in = getServletContext().getResourceAsStream("WEB-INF/classes/user.properties");

//      String path = getServletContext().getRealPath("WEB-INF/classes/user.properties");
//      InputStream in = new FileInputStream(new File(path));
//      Properties properties = new Properties();
//        properties.load(in);
//        String name = properties.getProperty("name");
//        String password = properties.getProperty("password");
//        System.out.println(name+":"+password);
        
        
        //----------读取WEB-INF/下目录下文件--------
        //方式1:
//      InputStream in = getServletContext().getResourceAsStream("WEB-INF/files/乔布斯.txt");
        
        //方式2:
        String path = getServletContext().getRealPath("WEB-INF/files/乔布斯.txt");
        InputStream in = new FileInputStream(new File(path));
        byte buffer[] = new byte[1024];
        int length = 0;
        while((length = in.read(buffer))!=-1){
            System.out.println(new String(buffer,0,length));
        }
    }

疑问:
这么多方法都可以获取资源,那么挑选哪个方法使用呢?
在可用的情况下,尽量使用(原因可能是线程安全问题)
Thread.currentThread().getContextClassLoader().getResource("");
Thread.currentThread().getContextClassLoader().getResource("/");

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