「java获取当前目录」java获取当前目录名

博主:adminadmin 2022-12-24 05:15:05 72

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

本文目录一览:

在java中如何取得当前工作目录

你好,提问者:

不知道我理解的是否是你想问的。如果解决了你的问题,请采纳,若有疑问,请追问。谢谢。

package com.gc.action.baiduTest;

import java.io.File;

public class FileName {

public static void main(String[] args) {

getFileName();

}

//取得当前目录的路径

public static void getFileName(){

File file =new File(".");

String path =file.getAbsolutePath();

System.out.println(path);

}

}

结果:

D:\ProgramFiles\eclipseWorkSpace\MySpring\.

java 怎么获取web根目录?

在java中获得文件的路径在我们做上传文件操作时是不可避免的。 web 上运行 1:this.getClass().getClassLoader().getResource("/").getPath(); this.getClass().getClassLoader().getResource("").getPath(); 得到的是 ClassPath的绝对URI路径。

以工程名为TEST为例:

(1)得到包含工程名的当前页面全路径:request.getRequestURI(),结果:/TEST/test.jsp。

(2)得到工程名:request.getContextPath(),结果:/TEST。

(3)得到当前页面所在目录下全名称:request.getServletPath()。结果:如果页面在jsp目录下 /TEST/jsp/test.jsp。

(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp"),结果D:/resin/webapps/TEST/test.jsp。

(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();

结果:D:/resin/webapps/TEST。

拓展:在类中取得路径的方法

(1)类的绝对路径:String u=Class.class.getClass().getResource("/").getPath()

结果:/D:/TEST/WebRoot/WEB-INF/classes/pack/

(2)得到工程的路径:System.getProperty("user.dir")

结果:D:/TEST

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获取当前目录和java获取当前目录名的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

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