Java获得当前目录代码示例
当前目录代码
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5131')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5131>import java.io.*;
public class Test {
public static void main(String[] args) {
System.out.println("Current Working Dir: " new File("").getAbsolutePath());
System.out.println("Current Working Dir: " System.getProperty("user.dir"));
}
}
获取当前jar包的路径
经过试验,不管是否是 Jar 包,不管是否是 Tomcat 部署,以下三个方法均可实现。
public class MyPath {
public static String getProjectPath() {
java.net.URL url = MyPath.class .getProtectionDomain().getCodeSource().getLocation();
String filePath = null ;
try {
filePath = java.net.URLDecoder.decode (url.getPath(), "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
if (filePath.endsWith(".jar"))
filePath = filePath.substring(0, filePath.lastIndexOf("/") 1);
java.io.File file = new java.io.File(filePath);
filePath = file.getAbsolutePath();
return filePath;
}
public static String getRealPath() {
String realPath = MyPath.class .getClassLoader().getResource("").getFile();
java.io.File file = new java.io.File(realPath);
realPath = file.getAbsolutePath();
try {
realPath = java.net.URLDecoder.decode (realPath, "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
return realPath;
}
public static String getAppPath(Class<?> cls){
//检查用户传入的参数是否为空
if (cls==null )
throw new java.lang.IllegalArgumentException("参数不能为空!");
ClassLoader loader=cls.getClassLoader();
//获得类的全名,包括包名
String clsName=cls.getName();
//此处简单判定是否是Java基础类库,防止用户传入JDK内置的类库
if (clsName.startsWith("java.")||clsName.startsWith("javax.")) {
throw new java.lang.IllegalArgumentException("不要传送系统类!");
}
//将类的class文件全名改为路径形式
String clsPath= clsName.replace(".", "/") ".class";
//调用ClassLoader的getResource方法,传入包含路径信息的类文件名
java.net.URL url =loader.getResource(clsPath);
//从URL对象中获取路径信息
String realPath=url.getPath();
//去掉路径信息中的协议名"file:"
int pos=realPath.indexOf("file:");
if (pos>-1) {
realPath=realPath.substring(pos 5);
}
//去掉路径信息最后包含类文件信息的部分,得到类所在的路径
pos=realPath.indexOf(clsPath);
realPath=realPath.substring(0,pos-1);
//如果类文件被打包到JAR等文件中时,去掉对应的JAR等打包文件名
if (realPath.endsWith("!")) {
realPath=realPath.substring(0,realPath.lastIndexOf("/"));
}
java.io.File file = new java.io.File(realPath);
realPath = file.getAbsolutePath();
try {
realPath=java.net.URLDecoder.decode (realPath,"utf-8");
}catch (Exception e){
throw new RuntimeException(e);
}
return realPath;
}//getAppPath定义结束
}
</td></tr></table>
使用Jar包,在Tomcat的运行结果如下:
ProjectPath: D:/J2EE/Tomcat 6.0/webapps/MyService1WebP/WEB-INF/lib
RealPath: D:/J2EE/Tomcat 6.0/webapps/MyService1WebP/WEB-INF/classes
Apppath: D:/J2EE/Tomcat 6.0/webapps/MyService1WebP/WEB-INF/classes
您可能感兴趣的文章:
java中获得当前文件路径多种方法
获取文件的绝对路径包含上一级目录的代码
C# Winform中获取文件路径的方法
1.14 Go语言工程结构详述
C#获取路径的多种方法
JSP入门教程(1)-基础知识
linux shell脚本启动java程序(图解)
php无限遍历目录代码
java.io.File中的绝对路径和相对路径.
python怎样嵌入java