使用servlet在属性文件的特定位置写入值

时间:2014-11-27 08:46:40

标签: java jsp servlets properties

**************这是我的控制器(伺服器)************************** **

        package com.igate.controller; 
        import java.io.FileNotFoundException;
        import java.io.IOException;
        import java.io.InputStream;
        import java.lang.reflect.Method;
        import java.util.ArrayList;
        import java.util.Properties;
        import javax.servlet.RequestDispatcher;
        import javax.servlet.ServletException;
        import javax.servlet.http.HttpServlet;
        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import org.apache.commons.configuration.ConfigurationException;
        import org.apache.commons.configuration.PropertiesConfiguration;

        public class TestController extends HttpServlet {
            private static final long serialVersionUID = 1L;
            public TestController() {                   
            }                           
            protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                doPost(request, response);
            }               
            protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
                System.out.println("TEst---------------------------------------------------------------------------");
                response.setContentType("text/html");
                String action= request.getParameter("action");
                System.out.println(action);
            if(action.equals("proceed"))
            {
                String pf =request.getParameter("pf");              
                System.out.println(pf);
          FileReader fr = new FileReader("D:/SCT_WRK/projectOne/src/framework.properties");
           BufferedReader br = new BufferedReader(fr);
          String line = "";
         String newTxt = "";
        while ((line = br.readLine()) != null){
             if ((line.trim()).equals("{PLATFORM_END}")){//if line has the match you need
                   line = pf+ "="+ pf; + "\n" + "{PLATFORM_END}";//insert new text
          }
               newTxt += line + "\n";
              FileWriter fw = new FileWriter ("D:/SCT_WRK/projectOne/src/framework.properties");
             BufferedWriter bw = new BufferedWriter(fw);
               bw.write(newTxt);
      br.close();
       bw.close();
      fr.close();
       fw.close();  
            }
            }
            }
        ---------------------------------------------------------------------------------------------

这是我的JSP PAGE ********

        <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
            pageEncoding="ISO-8859-1"%>
            <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

        <!DOCTYPE html>
        <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>
        </head>
        <body>
        <form method="post" action="TestController">
        <table align="center">
        <tr><th>Enter the Platform</th><td> <input type="text" name="pf"></td>
    <td><input type="submit" value="proceed" name="action" id ="p"> </td>
        </tr>
           </table>
        </form>
        </body>
        </html>
        ---------------------------------------------------------------------------------------------------       

这是我的财产文件******

        WORKING_DIR = com/test/auto/framework
        LIB = lib

        PLATFORM = {WINDOWS}
        APP_TYPE = {WEB}

        PLATFORM_START={START}
        WINDOWS = WINDOWS
        MAC = MAC
        ANDROID = ANDROID
        IOS = IOS
        PLATFORM_END={END}

        WEB = WEB
        NATIVE = NATIVE
        HYBRID = HYBRID


        PLATFORM_VERSION = 4.4.2
        DEVICE = GalaxyS4
        DEFAULT_URL = http://127.0.0.1:4723/wd/hub
        GRID_URL = http://localhost:4444/wd/hub

        IE_WEBDRIVER_PATH = {LIB}/IEDriverServer.exe
        CHROME_WEBDRIVER_PATH = {LIB}/chromedriver.exe
        IE_WEBDRIVER = IEDriverServer.exe
        CHROME_WEBDRIVER = chromedriver.exe

        BROWSER_PROFILE_PATH = {LIB}/{BROWSER}/Profile

        SAFARI_EXTENSION = {LIB}/{SAFARI}/Extension/extension.js
        FIREFOX_AUTOAUTH_EXTENSION = {LIB}/{FIREFOX}/Extension/autoauth-2.1-fx+fn.xpi

        USE_FIREFOX_AUTOAUTH = false

        EXEC_SPEED_CONTROLLER_SECONDS = 1

        LOG_WAIT_MESSAGES = false

    -----------------------------------------------------------------------------   

1)。我需要用户在平台输入文本中输入的内容应该在“{PLATFORM_END}”之前准确写入属性文件中         例子..用户输入“LINUX”         它应写在这里 - PLATFORM_START = {START}                                        WINDOWS = WINDOWS                                         MAC = MAC                                         ANDROID = ANDROID                                        IOS = IOS                         这里-------&GT; LINUX = LINUX                                       PLATFORM_END = {END}

2)。我希望所有用户输入的平台都应附加在平台列表底部和{PLATFORM_END}之前。

怎么可能。?如果有人知道逻辑,请帮助..     提前谢谢

1 个答案:

答案 0 :(得分:0)

不确定Apache公共配置是否已具备此功能。但是使用Java IO,您可以逐行读取配置文件,并通过添加新行重写每一行:

String pf = request.getParameter("pf");
    FileReader fr = new FileReader("D:/SCT_WRK/projectOne/src/framework.properties");
    BufferedReader br = new BufferedReader(fr);
    String line = "";
    String newTxt = "";
    while ((line = br.readLine()) != null)
    {
        if ((line.trim()).contains("PLATFORM_END={END}"))
        {// if line has the match you need
            line = pf + "=" + pf + "\n" + "PLATFORM_END={END}";// insert new
                                                            // text
        }
        newTxt += line + "\n";
    }

    FileWriter fw = new FileWriter("D:/SCT_WRK/projectOne/src/framework.properties");
    BufferedWriter bw = new BufferedWriter(fw);

    bw.write(newTxt);
    br.close();
    bw.close();
    fr.close();
    fw.close();