这两天做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可以解决问题,但我试了半天也没有能够解决。
无奈,最后向我一个老师求助。老师回复说【首先确定你的路径是否正确,其二,试试双斜杠,有些在处理路径的时候要这样,不认识单斜杠】。按照老师的指点又尝试一番,最后终于解决问题。
其实也比较简单,我把相关代码发上来供大家参考!
- <%@pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
- <%@pageimport="org.test.TestServlet,java.io.*"%>
- <%
- Stringpath=request.getContextPath();
- StringbasePath=request.getScheme()+"://"
- +request.getServerName()+":"+request.getServerPort()
- +path+"/";
- %>
- <%
- //读取配置文件
- Propertiesprops=newProperties();
- try
- {
- InputStreamin=TestServlet.class.getClassLoader()
- .getResourceAsStream("system.properties");
- props.load(in);
- }catch(IOExceptione)
- {
- System.out.println("配置文件读取失败!");
- }
- //获取配置信息
- Stringauthor=props.getProperty("author");
- if(author==null)
- author="";
- %>
- <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
- <html>
- <head>
- <basehref="<%=basePath%>">
- <title>MyJSP'index.jsp'startingpage</title>
- <metahttp-equiv="pragma"content="no-cache">
- <metahttp-equiv="cache-control"content="no-cache">
- <metahttp-equiv="expires"content="0">
- <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
- <metahttp-equiv="description"content="Thisismypage">
- <!--
- <linkrel="stylesheet"type="text/css"href="styles.css">
- -->
- </head>
- <body>
- <center>修改系统信息<br/>
- <formaction="test"method="post">
- <!--获取当前项目发布后在服务器的真实根路径-->
- <inputtype="hidden"name="realPath"
- value="<%=application.getRealPath("/")%>"/>
- <!--显示配置文件中的配置信息-->
- author:<inputtype="text"name="author"value="<%=author%>"/>
- <br/>
- <inputtype="submit"value="submit"/>
- </form>
- </center>
- </body>
- </html>
web.xml文件内容:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appversion="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文件内容:
- packageorg.test;
- importjava.io.File;
- importjava.io.FileOutputStream;
- importjava.io.IOException;
- importjava.io.OutputStream;
- importjava.util.Properties;
- importjavax.servlet.ServletException;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- publicclassTestServletextendsHttpServlet
- {
- /**
- *ThedoGetmethodoftheservlet.<br>
- *
- *Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
- *
- *@paramrequesttherequestsendbytheclienttotheserver
- *@paramresponsetheresponsesendbytheservertotheclient
- *@throwsServletExceptionifanerroroccurred
- *@throwsIOExceptionifanerroroccurred
- */
- publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
- throwsServletException,IOException
- {
- doPost(request,response);
- }
- /**
- *ThedoPostmethodoftheservlet.<br>
- *
- *Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
- *
- *@paramrequesttherequestsendbytheclienttotheserver
- *@paramresponsetheresponsesendbytheservertotheclient
- *@throwsServletExceptionifanerroroccurred
- *@throwsIOExceptionifanerroroccurred
- */
- publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
- throwsServletException,IOException
- {
- //Setsthecharacterencoding(MIMEcharset)oftheresponsebeingsenttotheclient
- response.setCharacterEncoding("UTF-8");
- StringrealPath=request.getParameter("realPath");
- Stringauthor=request.getParameter("author");
- //我原来两种错误的写法:
- //StringfilePath=realPath+"WEB-INF/classes/systeminfo.properties";
- //StringfilePath=realPath.replaceAll("//","/")+"WEB-INF/classes/systeminfo.properties";
- //下面是正确的写法:
- StringfilePath=realPath+"WEB-INF//classes//system.properties";
- Propertiesprop=newProperties();
- try
- {
- Filef=newFile(filePath);
- OutputStreamfos=newFileOutputStream(f);
- prop.setProperty("author",author);
- //将此Properties表中的属性列表(键和元素对)写入输出流。
- prop.store(fos,"Update'author'value");
- response.getWriter().write("系统信息更新成功!");
- }catch(IOExceptione)
- {
- System.err.println("Visit"+filePath+"forupdatingauthorvalueerror");
- e.printStackTrace();
- }
- }
- }
system.properties文件内容:
- #Update'author'value
- #ThuMay2613:00:59CST2011
- author=Tom
因为是一个测试例子,所以项目中的流程十分简单,我就不多废话了!
最后附上该测试例子源码(可直接导入MyEclipse 8.5):