java 中读取xml 和绝对路径

一.得到绝对路径

本人用到的不多,就列举常用的。

以上2种可读取固定配置文件可用得到绝对路径

1.System.getProperty("user.dir");
这个东西局限性十分高

一定要在运行了web程序并且配置了环境变量的环境中才可以得到,例如现在很多环境用myeclipse编写的自带jdk,结果路基就根本找不到。

顺便把System的东西都拿出来

public static void main(String[] args){ 
   System.out.println("Java运行时环境版本:/n"+System.getProperty("java.version")); 
   System.out.println("Java 运行时环境供应商:/n"+System.getProperty("java.vendor")); 
   System.out.println("Java 供应商的URL:/n"+System.getProperty("java.vendor.url")); 
   System.out.println("Java安装目录:/n"+System.getProperty("java.home")); 
   System.out.println("Java 虚拟机规范版本:/n"+System.getProperty("java.vm.specification.version")); 
   System.out.println("Java 类格式版本号:/n"+System.getProperty("java.class.version")); 
   System.out.println("Java类路径:/n"+System.getProperty("java.class.path")); 
   System.out.println("加载库时搜索的路径列表:/n"+System.getProperty("java.library.path")); 
   System.out.println("默认的临时文件路径:/n"+System.getProperty("java.io.tmpdir")); 
   System.out.println("要使用的 JIT 编译器的名称:/n"+System.getProperty("java.compiler")); 
   System.out.println("一个或多个扩展目录的路径:/n"+System.getProperty("java.ext.dirs")); 
   System.out.println("操作系统的名称:/n"+System.getProperty("os.name")); 
   System.out.println("操作系统的架构:/n"+System.getProperty("os.arch")); 
   System.out.println("操作系统的版本:/n"+System.getProperty("os.version")); 
   System.out.println("文件分隔符(在 UNIX 系统中是“/”):/n"+System.getProperty("file.separator")); 
   System.out.println("路径分隔符(在 UNIX 系统中是“:”):/n"+System.getProperty("path.separator")); 
   System.out.println("行分隔符(在 UNIX 系统中是“/n”):/n"+System.getProperty("line.separator")); 
   System.out.println("用户的账户名称:/n"+System.getProperty("user.name")); 
   System.out.println("用户的主目录:/n"+System.getProperty("user.home")); 
   System.out.println("用户的当前工作目录:/n"+System.getProperty("user.dir")); 
}

 

2.class.getResource("/").getPath());

tempPath.append(Mail.class.getResource("/").getPath());//获取当前class所在的路径
     if(tempPath.length()>0){//如果路径长度>0则路径存在
      tempPath.deleteCharAt(0);
      tempPath.delete(tempPath.length()-8, tempPath.length());//删除掉/classes
     }

这东西很十分好用,但是这个只能读单域的web服务器中的程序才可以得到,多域的无法得到。

我用到过的中间件。单域的tomcat  jboss  多域的weblogic  WebSphere

 

Servlet或者一些框架的action中得到绝对路径

3.servlet.getServletContext().getRealPath("/");

struts2 中ServletActionContext.getServletContext().getRealPath("/");

 

 

关于为什么要得绝对路径,那是因为读取web项目的一些外面类无法读取相对路径,比如说我们常用的读取xml

很多情况自己些的配置的东西是在web项目加载是启动的,而不是在servlet中读取的。

 

比如最常见的jdom方式读取xml。

1.用fileInputstream 读取 一般xml  这种方式只能读取绝对路径

例如下面的读取邮件配置的xml文件

一般自定义xml

[c-sharp]  view plain copy
  1. <?xml version="1.0" encoding="gb2312"?>  
  2. <onlineorder>  
  3. <address>  
  4.     <smtpusername>myzqf88@163.com</smtpusername>  
  5.     <smtppwd>111</smtppwd>  
  6.     <smtpserver>smtp.163.com</smtpserver>  
  7.   
  8.   </address>  
  9. </onlineorder>  

java代码

[java]  view plain copy
  1. SAXBuilder sb = new SAXBuilder(); // 新建立构造器  
  2. ocument doc1 = sb.build(new FileInputStream(path));  
  3.         Element root1=doc1.getRootElement();  
  4.         String smtpusername=""//用户名  
  5.         String smtppwd=""//密码  
  6.         String smtpserver=""//邮件服务器域名  
  7.         List list1=root1.getChildren("address");  
  8.        for(int ii=0;ii<list1.size();ii++){  
  9.        Element element=(Element)list1.get(ii);  
  10.        smtpusername=element.getChildText("smtpusername");       
  11.        smtppwd=element.getChildText("smtppwd");  
  12.        smtpserver=element.getChildText("smtpserver");  
  13.        }  

 

1.用InputStream 读取 一般xml  这种方式读取相对路径

jdm层次结构的xml

[c-sharp]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <data-services>  
  3.      <service name="longin">  
  4.         <sql>  
  5.             <!--[CDATA[  
  6.             SELECT count(*) FROM jcewk_com.dbo.BackPerson where  flag='0' and userid=? and password=?  
  7.             ]]-->  
  8.         </sql>  
  9.           <param-type>12</param-type>  
  10.      </service>  
  11. <service name="pzlist">  
  12.         <sql>  
  13.             <![CDATA[  
  14.          select id,title,committime,currstep,filenumber from jcewk_gwgl.dbo.handle where acceptuserid =? and (currstep ='待批转联合发文' or currstep='重新批转联合发文')  
  15.             ]]>  
  16.         </sql>  
  17.      <param-type>12</param-type>  
  18. </service>  
  19. </data-services>  

 

java

[c-sharp]  view plain copy
  1.   public static final String DNDI = "/dndi.xml";//包含各业务逻辑的SQL语句  
  2. //  其中/表示相对于web项目classes目录下  
  3. private void config() throws XMLException {  
  4.     services = new HashMap();  
  5.     InputStream is=this.getClass().getResourceAsStream(Constants.DNDI);  
  6.     xml = XMLUtil.getInsance(is);  
  7.     List serviceLists = xml.getAllElements("/data-services/service");  
  8.     Element each = null;  
  9.     String name = null;  
  10.     for (Iterator all = serviceLists.iterator(); all.hasNext(); ) {  
  11.       each = (Element) all.next();  
  12.       name = each.getAttributeValue("name").trim();  
  13.       if (services.containsKey(name))  
  14.         throw new XMLException("重复的配置:name = " + name);  
  15.       services.put(name, this.get_a_dndivo(each));  
  16.     }  
  17.   
  18.   }  
  19.   
  20.   private DndiVO get_a_dndivo(Element each) {  
  21.     DndiVO dndiVO = new DndiVO();  
  22.     dndiVO.setSql(each.getChildTextTrim("sql"));   //得到sql语句  
  23.     List params = each.getChildren("param-type");   //得到对应的数据连接池上线  
  24.     for (Iterator iter = params.iterator(); iter.hasNext(); ) {  
  25.       Element item = (Element) iter.next();  
  26.       dndiVO.addParams(item.getTextTrim());  
  27.     }  
  28.     return dndiVO;  
  29.   }  


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值