「java获取项目目录」java 读取目录

博主:adminadmin 2022-11-30 04:23:07 53

本篇文章给大家谈谈java获取项目目录,以及java 读取目录对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java如何获取项目的工作目录

import java.io.File;

import java.net.URLDecoder;

public class ProjectPath {

private static String rootPath = "";

private ProjectPath() {

init();

}

@SuppressWarnings("deprecation")

private static void init() {

String path = ProjectPath.class.getResource("ProjectPath.class")

.toString();

path = URLDecoder.decode(path);

path.replaceAll("\\\\", "/");

int index = path.indexOf("WEB-INF");

if (index == -1) {

index = path.indexOf("bin");

}

if (index == -1) {

index = path.indexOf("lib");

}

if (index == -1) {

int index2 = path.indexOf("jar!");

if (index2 != -1) {

path = path.substring(0, index2);

System.out.println(path);

System.out.println(File.separator);

index = path.lastIndexOf("/");

System.out.println(index);

}

}

path = path.substring(0, index);

if (path.startsWith("jar")) {

path = path.substring(9);

}

if (path.startsWith("file")) {

path = path.substring(5);

}

if (path.endsWith("/") || path.endsWith("\\")) {

path = path.substring(0, path.length() - 1);

}

// linux环境下第一个/是需要的

String os = System.getProperty("os.name").toLowerCase();

if (os.startsWith("win")) {

path = path.substring(1);

}

rootPath = path;

}

/**

* 获取应用的根目录,路径分隔符为/,路径结尾无/

*

* @return

*/

public static String getProjectPath() {

if ("".equals(rootPath)) {

init();

}

return rootPath;

}

}

java windows和linux获取项目根目录的方法一致吗?

java有个特性是跨平台性,所以其获取项目根目录的方法是一样的。

request.getContextPath()方法就是是得到项目的名字,如果项目为根目录,则得到一个"",即空的字条串。如果项目为abc,%=request.getContextPath()% 将得到abc,服务器端的路径则会自动加上,a href="XXXX.jsp" 是指当前路径下的这个xxx.jsp页面,有时候也可以在head里设置html:base来解决路径的问题,不多用的最多的还是request.getContextPath。

java 项目如何获取项目所在的物理根路径

在java中获得文件的路径在我们做上传文件操作时是不可避免的。web上运行1:this.getClass().getClassLoader().getResource("/").getPath();this.getClass().getClassLoader().getResource("").getPath();得到的是ClassPath的绝对URI路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/System.getProperty("user.dir");this.getClass().getClassLoader().getResource(".").getPath();得到的是项目的绝对路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war2:this.getClass().getResource("/").getPath();this.getClass().getResource("").getPath();得到的是当前类文件的URI目录。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/this.getClass().getResource(".").getPath();X不能运行3:Thread.currentThread().getContextClassLoader().getResource("/").getPath()Thread.currentThread().getContextClassLoader().getResource("").getPath()得到的是ClassPath的绝对URI路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/Thread.currentThread().getContextClassLoader().getResource(".").getPath()得到的是项目的绝对路径。如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war在本地运行中1:this.getClass().getClassLoader().getResource("").getPath();this.getClass().getClassLoader().getResource(".").getPath();得到的是ClassPath的绝对URI路径。如:/D:/myProjects/hp/WebRoot/WEB-INF/classesthis.getClass().getClassLoader().getResource(".").getPath();X不能运行2:this.getClass().getResource("").getPath();this.getClass().getResource(".").getPath();得到的是当前类文件的URI目录。如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper//D:/myProjects/hp/WebRoot/WEB-INF/classes/得到的是ClassPath的绝对URI路径。如:/D:/myProjects/hp/WebRoot/WEB-INF/classes3:Thread.currentThread().getContextClassLoader().getResource(".").getPath()Thread.currentThread().getContextClassLoader().getResource("").getPath()得到的是ClassPath的绝对URI路径。。如:/D:/myProjects/hp/WebRoot/WEB-INF/classesThread.currentThread().getContextClassLoader().getResource("/").getPath()X不能运行最后在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。还有request.getContextPath();在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。

JAVA中如何得到文件路径

java文件中获得路径

Thread.currentThread().getContextClassLoader().getResource("") //获得资源文件(.class文件)所在路径

ClassLoader.getSystemResource("")

Class_Name.class.getClassLoader().getResource("")

Class_Name.class .getResource("/")

Class_Name.class .getResource("") // 获得当前类所在路径

System.getProperty("user.dir") // 获得项目根目录的绝对路径

System.getProperty("java.class.path") //得到类路径和包路径

打印输出依次如下:

file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/

file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/

file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/

file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/

file:/F:/work_litao/uri_test/WebContent/WEB-INF/classes/com/xml/imp/

F:\work_litao\uri_test

F:\work_litao\uri_test\WebContent\WEB-INF\classes;F:\work_litao\uri_test\WebContent\WEB-INF\lib\dom4j.jar

通过java获取当前项目路径

getClass().getResource() 方法获得相对路径( 此方法在jar包中无效。返回的内容最后包含/)

例如 项目在/D:/workspace/MainStream/Test

在javaProject中,getClass().getResource("/").getFile().toString() 返回:/D:/workspace/MainStream/Test/bin/

public String getCurrentPath(){  

       //取得根目录路径  

       String rootPath=getClass().getResource("/").getFile().toString();  

       //当前目录路径  

       String currentPath1=getClass().getResource(".").getFile().toString();  

       String currentPath2=getClass().getResource("").getFile().toString();  

       //当前目录的上级目录路径  

       String parentPath=getClass().getResource("../").getFile().toString();  

         

       return rootPath;         

  

   }

参考资料:

关于java获取项目目录和java 读取目录的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-30,除非注明,否则均为首码项目网原创文章,转载请注明出处。