这两天做Web项目时需要向配置文件中写入信息,可是调试良久未果。具体情况如下:
Web项目testproject中需要向src下的一个配置文件system.properties中写入信息。我在页面中通过String realPath = application.getRealPath("/")方式可以获取当前项目发布后的根路径(即D:/ToolSoft/apache-tomcat-6.0.32/webapps/testproject/)。而那个配置文件在项目下的路径为WEB-INF/classes/system.properties。我想以new File(realPath + “WEB-INF/classes/system.properties”);的方式创建一个文件对象并向其中写入信息,可是程序执行时出错。
我觉得应该是那个路径和java里面的路径分隔符不一致的问题。于是,有人建议我直接在创建File对象时把路径写成固定的。可我觉得这样不好,因为项目重新发布时还要修改该路径。上网找了一下,有人说用File.separator可以解决问题,但我试了半天也没有能够解决。
无奈,最后向我一个老师求助。老师回复说【首先确定你的路径是否正确,其二,试试双斜杠,有些在处理路径的时候要这样,不认识单斜杠】。按照老师的指点又尝试一番,最后终于解决问题。
其实也比较简单,我把相关代码发上来供大家参考!
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ page import="org.test.TestServlet,java.io.*"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://"
- + request.getServerName() + ":" + request.getServerPort()
- + path + "/";
- %>
- <%
- //读取配置文件
- Properties props = new Properties();
- try
- {
- InputStream in = TestServlet.class.getClassLoader()
- .getResourceAsStream("system.properties");
- props.load(in);
- } catch (IOException e)
- {
- System.out.println("配置文件读取失败!");
- }
- //获取配置信息
- String author = props.getProperty("author");
- if (author == null)
- author = "";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <center>修改系统信息<br/>
- <form action="test" method="post">
- <!-- 获取当前项目发布后在服务器的真实根路径 -->
- <input type="hidden" name="realPath"
- value="<%=application.getRealPath("/")%>" />
- <!-- 显示配置文件中的配置信息 -->
- author: <input type="text" name="author" value="<%=author%>" />
- <br/>
- <input type="submit" value="submit" />
- </form>
- </center>
- </body>
- </html>
web.xml文件内容:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <servlet>
- <servlet-name>TestServlet</servlet-name>
- <servlet-class>org.test.TestServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>TestServlet</servlet-name>
- <url-pattern>/test</url-pattern>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
TestServlet.java文件内容:
- package org.test;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Properties;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class TestServlet extends HttpServlet
- {
- /**
- * The doGet method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to get.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- doPost(request, response);
- }
- /**
- * The doPost method of the servlet. <br>
- *
- * This method is called when a form has its tag value method equals to post.
- *
- * @param request the request send by the client to the server
- * @param response the response send by the server to the client
- * @throws ServletException if an error occurred
- * @throws IOException if an error occurred
- */
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- // Sets the character encoding (MIME charset) of the response being sent to the client
- response.setCharacterEncoding("UTF-8");
- String realPath = request.getParameter("realPath");
- String author = request.getParameter("author");
- // 我原来两种错误的写法:
- // String filePath = realPath + "WEB-INF/classes/systeminfo.properties";
- // String filePath = realPath.replaceAll("//", "/") + "WEB-INF/classes/systeminfo.properties";
- // 下面是正确的写法:
- String filePath = realPath + "WEB-INF//classes//system.properties";
- Properties prop = new Properties();
- try
- {
- File f = new File(filePath);
- OutputStream fos = new FileOutputStream(f);
- prop.setProperty("author", author);
- // 将此 Properties 表中的属性列表(键和元素对)写入输出流。
- prop.store(fos, "Update 'author' value");
- response.getWriter().write("系统信息更新成功!");
- } catch (IOException e)
- {
- System.err.println("Visit " + filePath + " for updating author value error");
- e.printStackTrace();
- }
- }
- }
system.properties文件内容:
- #Update 'author' value
- #Thu May 26 13:00:59 CST 2011
- author=Tom
因为是一个测试例子,所以项目中的流程十分简单,我就不多废话了!
最后附上该测试例子源码(可直接导入MyEclipse 8.5):