您的位置:程序门 -> java -> j2se / 基础类



如何获取java项目的绝对路径


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何获取java项目的绝对路径[已结贴,结贴人:caileisoft1000]
发表于:2007-01-10 11:29:21 楼主
如何获取java项目的绝对路径
发表于:2007-01-10 12:58:361楼 得分:20
给楼主一个我写的例子,希望有所帮助:

import   java.io.file;
import   java.util.iterator;
import   java.util.list;

import   org.dom4j.document;
import   org.dom4j.documentexception;
import   org.dom4j.element;
import   org.dom4j.io.saxreader;

public   class   xmlread   {

private   static   string   xmlurl;
       
/**
  *   初始化
  *
  */
public   xmlread()   {
string   path   =   getclass().getprotectiondomain().getcodesource()
.getlocation().getpath();

if   (path.indexof( "web-inf ")   >   0)   {
path   =   path.substring(0,   path.indexof( "/web-inf/ ")   +   9);

}
this.xmlurl   =   path;

}
       
/**
  *   根据参数名获得对应参数的值
  *   @param   name
  *   @return
  */
public   static   string   getxmlvaluebyname(string   name)   {
xmlread   xmlread=new   xmlread();
 
string   value   =   null;
if   (name   !=   null)   {
saxreader   reader   =   new   saxreader();
try   {
document   document   =   reader.read(new   file(xmlread.xmlurl
+   "/ttms-config.xml "));
list   list   =   document.selectnodes( "/root/element ");//   读取配置文件
iterator   iter   =   list.iterator();
while   (iter.hasnext())   {
element   element   =   (element)   iter.next();
string   elementname   =   element.elementtext( "name ");
if   (elementname.equals(name))   {
value   =   element.elementtext( "value ");
break;
}
}

}   catch   (documentexception   e)   {

e.printstacktrace();
return   null;
}
}
return   value;
}


}
发表于:2007-01-10 13:00:062楼 得分:0
xmlurl属性应该就是楼主要的东东
发表于:2007-01-10 13:21:173楼 得分:0
getservletcontext().getrealpath( "/ ")?
发表于:2007-01-10 13:28:374楼 得分:0
getservletcontext().getrealpath( "/ ")
同意楼上的~
不过这个是在jsp里的
不知道lz要的是不是这个东西?
发表于:2007-01-10 15:26:545楼 得分:0
getservletcontext().getrealpath( "/ ")
发表于:2007-01-10 17:01:396楼 得分:0
如果是jsp的,象楼上说的
如果是普通的项目,用system.getproperty( "user.dir ");
发表于:2007-01-10 17:20:417楼 得分:0
在一个xml里写好,然后读出来.

貌似只能hard   code吧


快速检索

最新资讯
热门点击