0

How to append some text in div with id="sw" and befor "<-- adding some thing here />" and write in response output in JSP ?

"index_012.html content is:

    <html><head></head><body>
<div id="maincontent" class="maincontent">
    <section id="mainthecontent">
        <div id="swRight"></div>
        <div id="sw" >
            <!--adding some thing here-->
        </div>                                
        <div class="clear"></div>
    </section>
    <div class="clear"></div>
</div>
</body></html>

my jsp code is :

    FileReader fr = new FileReader("index_012.html");
    BufferedReader br = new BufferedReader(fr);     
    StringBulder addingContent= "<div>Main Content</div>";
    StringBulder result="";
    String line = null;
    // finding place to adding StringBulder
    int contentPlace = -1;
    while((line=br.readLine()) != null && contentPlace==-1){
        int swIndex = line.indexOf("\"sw\"");
        contentPlace = line.indexOf(">",swIndex);
    }
if(contentPlace>-1){//contentPlace is index of where i must append "addingContent"

--------i need help here ----------
result = br.append(addingContent , contentPlace);
--------i need help here ----------
}

and then this jsp tag

<%= result %>

I don't need any change in index_012.html or any other file

write in response output

in Other word how to read file as StringBulder --> replace somthing --> write to response ??

m s
  • 65
  • 1
  • 9

2 Answers2

0

You need a BufferedWriter for write in a file.

Is better if you open a temporary file in writing mode, then you can delete the original file and rename the temporary file.

I think you need something like https://stackoverflow.com/a/8563446/3785263

Community
  • 1
  • 1
BlackBlaze
  • 254
  • 2
  • 10
  • I don't need any change in index_012.html and dont need save file, read content, in object, add something to it, wite to response not file, write to out.right like <%= result %> – m s Mar 17 '15 at 16:07
0
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% StringBulder addingContent= "<div>Main Content</div>"; %>
<% FileReader fr = new FileReader("G:\\index_jeymarble.html");%>
<% BufferedReader br = new BufferedReader(fr);%>
<% String line = null;%><% while ((line = br.readLine()) != null) {%>
<%= line%><%    if (line.indexOf("\"sw\"")>-1 ) { %><%= addingContent %>
<%}%>
<%}%>

If any one has any idea to use regexp comment And there is some problem in pageEncoding.

m s
  • 65
  • 1
  • 9