package com.bd.test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class csDao {
public static void main(String[] args)
{
csDao getclasspath = new csDao();
System.out.println(getclasspath.getClassPath());
File file = new File("index.html");
try {
System.out.println("获得webroot下文件的路径"+file.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*在类中取得当前文件所在的相对路径与绝对路径
*
* @return String
*/
public String getClassPath()
{
String strClassName = getClass().getName();
String strPackageName = "";
if(getClass().getPackage() != null)
{
strPackageName = getClass().getPackage().getName();
}
System.out.println("ClassName:" + strClassName);
System.out.println("PackageName:" + strPackageName);
String strClassFileName = "";
if(!"".equals(strPackageName))
{
strClassFileName = strClassName.substring(strPackageName.length() + 1,strClassName.length());
}
else
{
strClassFileName = strClassName;
}
System.out.println("ClassFileName:" + strClassFileName);
URL url = null;
url = getClass().getResource(strClassFileName + ".class");
String strURL = url.toString();
strURL = "获得类的路径"+strClassFileName+"物理路径:"+ strURL.substring(strURL.indexOf('/') + 1,strURL.lastIndexOf('/'));
return strURL;
}
}