Solved

how to append text to a text file?

Posted on 2003-11-01
7
1,717 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
7 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
Comment Utility
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
Comment Utility
>>BufferedWriter bw = new BufferedWriter(new FileWriter("/temp/user_date.txt"));
try
  FileWriter ("/temp/user_date.txt", true);



0
 
LVL 15

Expert Comment

by:jimmack
Comment Utility
;-)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:f_98
Comment Utility
sorry; posted in parallel
0
 
LVL 15

Expert Comment

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

Author Comment

by:saleemkhan
Comment Utility
;-)

thanks
0
 
LVL 3

Expert Comment

by:applekanna
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now