Solved

how to append text to a text file?

Posted on 2003-11-01
7
1,732 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
Independent Software Vendors: 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article describes how to import Lotus Notes Contacts into Outlook 2016, 2013, 2010 and 2007 etc. with a few manual steps. You can easily export and migrate Lotus Notes contacts into Microsoft Outlook without having to use any third party tools.
Part One of the two-part Q&A series with MalwareTech.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

617 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