hyy9512

导航

文件6. 查找替换.txt文本文件中的内容

servlet实现对文本文件的查找替换

.jsp界面

 1 <form>
 2               <table>
 3           <tr>
 4               <td>选择文本文件:</td>
 5               <td><input type="text" name="filesPath" /></td>
 6           </tr>
 7           <tr>
 8               <td>搜索文本:</td>
 9               <td><input type="text" name="content" /></td>
10           </tr>
11           <tr>
12               <td>替换文本:</td>
13               <td><input type="text" name="replace" /></td>
14           </tr>
15           <tr>
16               <td colspan="2">
17                   <input type="button"  value="提交"/>
18               </td>
19           </tr>
20       </table>
21       </form>

.js代码

 1 $().ready(function(){
 2     $("input[type='button']").click(function(){
 3         var filesPath=$("input[name='filesPath'").val();
 4         var con=$("input[name='content']").val();
 5         var replace=$("input[name='replace']").val();
 6         var xmlhttp=getXmlhttp();
 7         var data={"filesPath":filesPath,"content":con,"replace":replace};
 8         xmlhttp.onreadystatechange=function() {
 9             if(xmlhttp.readyState==4 && xmlhttp.status==200) {
10                 alert("转换完成!");
11             } 
12         };
13         xmlhttp.open("POST","showFile","true");
14         xmlhttp.setRequestHeader("Content-Type","application/json");
15         xmlhttp.send(JSON.stringify(data));
16     });
17 });
18 //获取xmlHttp
19 function getXmlhttp() {
20     var xmlhttp;
21     if(window.XMLHttpRequest) {
22         xmlhttp=new XMLHttpRequest;
23     } else {
24         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
25     }
26     return xmlhttp;
27 }

servlet层ShowSomeType.java

 1 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
 2             throws ServletException, IOException {
 3         req.setCharacterEncoding("utf-8");
 4         resp.setCharacterEncoding("utf-8");
 5         resp.setContentType("text/html;charset=utf-8");
 6         JSONObject data=getJsonObject(req);
 7         String filesPath=data.getString("filesPath");
 8         String content=data.getString("content");
 9         String replace=data.getString("replace");
10         boolean seccess=false;
11         if(filesPath!=null && content!=null && replace!=null) 
12             seccess=replaceFileStr(filesPath,content,replace);
13         System.out.println(seccess);
14     }
15     
16     private boolean replaceFileStr(String path, String str, String con) {
17         try {
18             FileReader fr=new FileReader(path);          //创建文件输入流
19             BufferedReader br=new BufferedReader(fr);       
20             char[] data=new char[1024];               //创建缓冲字符数组
21             int rn=0;
22             StringBuilder sb=new StringBuilder();         //创建字符串构件器
23             while((rn=fr.read(data))>0) {              //读取文件内容到字符串构件器
24                 String content=String.valueOf(data,0,rn);     
25                 sb.append(content);
26             }
27             fr.close();
28             String contentStr=sb.toString().replace(str,con);  //从构件器中生成字符串,并替换搜索文本
29             FileWriter font=new FileWriter(path);         //创建文件输出流
30             font.write(contentStr.toCharArray());         //把替换完成的字符串写入文件内
31             font.close();                       //关闭输出流
32             return true;
33         } catch (FileNotFoundException e) {
34             e.printStackTrace();
35             return false;
36         } catch (IOException e) {
37             e.printStackTrace();
38             return false;
39         }
40     }
41 
42     private JSONObject getJsonObject(HttpServletRequest req) {
43         StringBuffer json=new StringBuffer();
44         String lineString=null;
45         BufferedReader reader;
46         JSONObject data=null;
47         try {
48             reader = req.getReader();
49             while((lineString=reader.readLine())!=null) {
50                 json.append(lineString);
51             }
52              data=JSONObject.fromObject(json.toString());
53         } catch (IOException e) {
54             e.printStackTrace();
55         }
56         return data;
57     }

 

posted on 2017-12-17 10:20  hyy9512  阅读(1407)  评论(0编辑  收藏  举报