Solved

how to append text to a text file?

Posted on 2003-11-01
7
1,731 Views
Last Modified: 2012-05-04
hi all,
i want to append text to a file submitting from a jsp page.

i have a search.jsp
code in search.jsp

while (rs.next())
{
%>
<tr>
<td><a href="details.jsp?d_id=<%=rs.getInt("d_id")%>"><%=rs.getString("name")%></a>
</td>
</tr>
<%}%>

so for each record it creats a link when click on each link it will display the details.

CODE IN DETAILS.JSP
<!-- Name -->
<TR>
<TD HEIGHT="20" nowrap><b><FONT COLOR="3366CC" SIZE="2" FACE="Tahoma">name </FONT></b></TD>
<TD HEIGHT="20"><FONT FACE="Courier New" color="blue"><b><INPUT NAME="name" VALUE="<%=rs.getString("d_name")%>" SIZE="20" MAXLENGTH="50"></b></FONT></TD>
</TR>


<!-- Civilid -->
<TR>
<TD HEIGHT="20" nowrap><b><FONT COLOR="3366CC" SIZE="2" FACE="Tahoma">CIVILID</FONT></b></TD>
<TD HEIGHT="20"><FONT FACE="Arial"><INPUT NAME="civilid" VALUE="<%=(rs.getString("CIVIL_ID") == null? "":rs.getString("CIVIL_ID"))%>" SIZE="20" MAXLENGTH="12"></FONT></TD>
</TR>

HERE USER CAN MODIFY NAME AND CIVILID FIELDS AND UPDATE.

CODE IN UPDATE.JSP
sql="UPDATE TABLENAME SET "name='" + name + "'," + "CIVIL_ID='" + civilid + "',"+ "'WHERE id='" + d_id + "'";
TILL NOW EVERYTHING OK.

MY PROBLEM IS I WANT TO STORE ALL THE OLD DATA BEFORE UPDATE AND AFTER UPDATE INTO A TEXT FILE SO THAT I CAN KNOW THAT WHAT HAS BEEN UPDATED.

SO I TRIED LIKE IN UPDATE.JSP AFTER UPDATE SYNTAX I AM USING THE BELOW CODE.
THE PROBLEM IS HERE ITS NOT APPENDING THE NEW VALUES.I JUST OVERWRITING THE FILE.

SO ANY HELP.




<%

BufferedWriter bw = new BufferedWriter(new FileWriter("/temp/user_date.txt"));
Enumeration enum = request.getParameterNames();
String line = "";
while (enum.hasMoreElements()) {
String paramName = (String)enum.nextElement();
line = paramName + ": ";
String[] values = request.getParameterValues(paramName);
for (int i = 0; i < values.length; i++) {
line += values;
if (i != (values.length - 1)) line += ", ";
}
bw.write(line, 0, line.length());
bw.newLine();
}
bw.close();
%>
0
Comment
Question by:saleemkhan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
ID: 9662519
For the line:

BufferedWriter bw = new BufferedWriter(new FileWriter("/temp/user_date.txt"));

add a second parameter to the FileWriter as follows:

BufferedWriter bw = new BufferedWriter(new FileWriter("/temp/user_date.txt", true));

This will then append to the end of the file instead of overwriting ;-)
0
 
LVL 2

Expert Comment

by:f_98
ID: 9662524
>>BufferedWriter bw = new BufferedWriter(new FileWriter("/temp/user_date.txt"));
try
  FileWriter ("/temp/user_date.txt", true);



0
 
LVL 15

Expert Comment

by:jimmack
ID: 9662525
;-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:f_98
ID: 9662526
sorry; posted in parallel
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9662530
No problem.  It's always a race with questions like this ;-)
0
 

Author Comment

by:saleemkhan
ID: 9665383
;-)

thanks
0
 
LVL 3

Expert Comment

by:applekanna
ID: 9667650
Wouldn't "write" accessing a file from a JSP cause a thread issue coz write does a "write file lock" on the file .
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The conference as a whole was very interesting, although if one has to make a choice between this one and some others, you may want to check out the others.  This conference is aimed mainly at government agencies.  So it addresses the various compli…
Let’s face it: one of the reasons your organization chose a SaaS solution (whether Microsoft Dynamics 365, Netsuite or SAP) is that it is subscription-based. The upkeep is done. Or so you think.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question