java操作Properties属性文件及获取项目部署服务器路径

来源:互联网 发布:java经纬度计算距离 编辑:程序博客网 时间:2024/05/16 05:31
</pre>一、web项目根目录的获得(发布之后),java原生类中不可以使用1 从servlet出发可建立一个servlet在其的init方法中写入如下语句(没有请求的话会抛空指针导常)<pre name="code" class="java">ServletContext s1=this.getServletContext();String temp=s1.getRealPath("/"); (关键) 


结果形如:F:\tomcat-6.0.36\webapps\test\(test为项目名字)
如果是调用了s1.getRealPath("")则输出F:\tomcat-6.0.36\webapps\test(少了一个"\")

2 从httpServletRequest出发(没有请求的话会抛空指针导常)
String path=request.getSession().getServletContext().getRealPath("/");
结果形如: F:\tomcat-6.0.36\webapps\test\


二、属性文件的读取: 
 1、filePath为.properties必须为绝对路径,相对路径找不到该文件
 Properties props = new Properties();   InputStream in = lnew BufferedInputStream( new FileInputStream(filePath)); p.load(in);  String realPath=props.getProperty("key");  

 2、filePath可为.properties相对路径
 Properties props = new Properties();   props.load(NewUserController.class.getClassLoader().getResourceAsStream(filename)); String realPath=props.getProperty("key");  
 ps:在读写操作文件时,使用第一种方式做为输入流(InputStream)可以读写操作,第二种方式仅限于读取输入流(InputStream),而写入不到文件中,但是不报错的,同时会将你相对路径的文件拷贝到tomcat所在盘符根目录下建立文件。我tomcat在E盘所以会在E盘,则将我项目中相对路径文件拷贝到E盘下,之后修改这个文件!


三、获取服务器路径的各种方式
        //获取服务器中绝对地址,带file: file:/D:/wc/.metadata/.plugins/org.eclipse.wst.server.core/tmp7/wtpwebapps/wanxue/WEB-INF/classes/System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));System.out.println(this.getClass().getResource("/"));//获取服务器中绝对地址 /E:/workSoftware/apache-tomcat-7.0.56/webapps/wanxue/WEB-INF/classes/        System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());//获取当前类的在服务器中绝对地址 /E:/workSoftware/apache-tomcat-7.0.56/webapps/wanxue/WEB-INF/classes/        System.out.println(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());        //获取当前项目在部署在服务器的绝对位置,并去掉路径前的下划线  E:/workSoftware/apache-tomcat-7.0.56/webapps/wanxue/WEB-INF/classes/        String path = switchDServiceImpl.class.getClassLoader().getResource("") .getPath().substring(1);  

ps:这个服务器地址为你在eclipse中配置Apache Tomcat中的部署路径,默认路径为 workspace/.metadata/.plugins/org.eclipse.core.resources/.projects
ps:修改后Eclipse中项目在Apache Tomcat中的部署路径,地址:点击打开链接
ps:做项目过程中,一定要区分eclipse的默认项目Apache Tomcat的部署路径,自己的项目工作目录及自己安装Apache Tomcat安装目录,以免混淆。


四、获取其他路径地址
//tomcat根目录  E:\workSoftware\apache-tomcat-7.0.56System.out.println(System.getProperty("catalina.home").toString());//eclipse根目录地址  E:\workSoftware\eclipse        System.out.println(System.getProperty("user.dir"));        System.out.println((new File("")).getAbsolutePath());        System.out.println(System.getProperty("user.dir").replace("bin", "webapps"));



五、JAVA读写操作代码
<span style="white-space:pre"></span>//开关配置地址,获取当前项目在部署在服务器的绝对位置static String filePath =switchDServiceImpl.class.getClassLoader().getResource("") .getPath().substring(1)+"switchState.properties"; /** * @author 王超 * return 根据key取得value值 */  public  String keyValue(String key) {Properties pps = new Properties();try {InputStream in = new BufferedInputStream (new FileInputStream(filePath));  pps.load(in);String value = pps.getProperty(key);return value;}catch (Exception e) {e.printStackTrace();return "switchState.properties路径找不到";}}/** * 修改properties中键值对 * @author 王超 * @param  key 键 * @param  value 值  */public  void writeProperties( String key, String value) {Properties pps = new Properties();InputStream in;try {in = new FileInputStream(filePath);//从输入流中读取属性列表(键和元素对) pps.load(in);//调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。  //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。OutputStream out = new FileOutputStream(filePath);pps.setProperty(key, value);//以适合使用 load 方法加载到 Properties 表中的格式,  //将此 Properties 表中的属性列表(键和元素对)写入输出流  pps.store(out, "Update " + key + " name");} catch (Exception e) {e.printStackTrace();}}

ps:这里采用new BufferedInputStream (new FileInputStream(filePath))来进行读写文件操作,在"三"中有介绍的
ps:调用keyValue(key)读取文件,调用writeProperties(key,value)进行添加或修改,当key存在则修改,不存在则添加键值对!
ps:在java项目中可以使用File.separator代表下划线


0 0
原创粉丝点击