Solved

how to append text to a text file?

Posted on 2003-11-01
7
1,729 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

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!

Question has a verified solution.

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

The following article is comprised of the pearls we have garnered deploying virtualization solutions since Virtual Server 2005 and subsequent 2008 RTM+ Hyper-V in standalone and clustered environments.
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

738 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