「java获取运行目录」java 读取目录

博主:adminadmin 2023-01-18 05:48:07 398

今天给各位分享java获取运行目录的知识,其中也会对java 读取目录进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

通过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获取程序运行的当前工作目录

使用下面这个PathUtil的getProgramPath()就可以获得当前程序运行的目录。

import java.net.URL;

import java.net.URLDecoder;

class PathUtil {

/**

* Get the env of windir, such as "C:\WINDOWS".

*

* @return the env of windir value.

*/

public static String getWindir() {

return System.getenv("windir");

}

/**

* Get file separator, such as "/" on unix.

*

* @return the separator of file.

*/

public static String getFileSeparator() {

return System.getProperty("file.separator");

}

/**

* Get line separator, such as "\n" on unix.

*

* @return the separator of line.

*/

public static String getLineSeparator() {

return System.getProperty("line.separator");

}

/**

* Get programPath

*

* @return programPath

*/

public static String getProgramPath() {

ClassPathUtil cls = PathUtil.class;

ClassLoader loader = cls.getClassLoader();

//

// Get the full name of the class.

//

String clsName = cls.getName() + ".class";

//

// Get the package that include the class.

//

Package pack = cls.getPackage();

String path = "";

//

// Transform package name to path.

//

if (pack != null) {

String packName = pack.getName();

//

// Get the class's file name.

//

clsName = clsName.substring(packName.length() + 1);

//

// If package is simple transform package name to path directly,

// else transform package name to path by package name's

// constituent.

//

path = packName;

if (path.indexOf(".") 0) {

path = path.replace(".", "/");

}

path = path + "/";

}

URL url = loader.getResource(path + clsName);

//

// Get path information form the instance of URL.

//

String retPath = url.getPath();

//

// Delete protocol name "file:" form path information.

//

try {

int pos = retPath.indexOf("file:");

if (pos -1) {

retPath = retPath.substring(pos + 5);

}

//

// Delete the information of class file from the information of

// path.

//

pos = retPath.indexOf(path + clsName);

retPath = retPath.substring(0, pos - 1);

//

// If the class file was packageed into JAR e.g. file, delete the

// file name of the corresponding JAR e.g..

//

if (retPath.endsWith("!")) {

retPath = retPath.substring(0, retPath.lastIndexOf("/"));

}

retPath = URLDecoder.decode(retPath, "utf-8");

} catch (Exception e) {

retPath = null;

e.printStackTrace();

}

return retPath;

}

}

测试类:

public class Test{

public static void main(String args[]){

String s = PathUtil.getProgramPath();

System.out.println(s);

}

}

java中如何获得运行中的.class文件的存放目录的绝对路径?

1、资源文件放在服务器下是完全没问题的,一个网站发布后也不会随便更新的。

2、如果资源文件过多、或都过大,是建议放到服务器下的,会占用服务器过大的空间,你可以在tomcat中再配置一个虚拟路径,指向一个盘符下一个文件夹(如:d:/images),在

tomcat

server.xml

中设置,然后你就可以用你的服务器地址+/img/+资源路径(注意:这个资源路径是相对d:/images的相对路径)去访问资源了

具体设置方法:

在conf目录下的server.xml文件里的里面加入

例如:

context

path=”test”

docbase=”f:\webroot\”/

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