Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1740
  • Last Modified:

how to append text to a text file?

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
saleemkhan
Asked:
saleemkhan
1 Solution
 
jimmackCommented:
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
 
f_98Commented:
>>BufferedWriter bw = new BufferedWriter(new FileWriter("/temp/user_date.txt"));
try
  FileWriter ("/temp/user_date.txt", true);



0
 
jimmackCommented:
;-)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
f_98Commented:
sorry; posted in parallel
0
 
jimmackCommented:
No problem.  It's always a race with questions like this ;-)
0
 
saleemkhanAuthor Commented:
;-)

thanks
0
 
applekannaCommented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now