This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Servlets and the fly likes saving data and appending to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "saving data and appending to file" Watch "saving data and appending to file" New topic

saving data and appending to file

kalyani anne

Joined: Jul 24, 2001
Posts: 5
thanks Bosun.I got the solution,but now i have another problem,i want to save a employee's records in thst file.but
here when i add second record first one is getting deleted and the new one is replaced.
can anyone help me in that i can save the records and append to the same file
here ie the code
public class kal1 extends HttpServlet

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
PrintWriter toClient = res.getWriter();

try {

File outputFile = new File("myfile");
FileWriter in = new FileWriter(outputFile);
BufferedWriter bf = new BufferedWriter(in);

String surveyName = req.getParameterValues("kal1")[0];
PrintWriter toFile = new PrintWriter(in);

Enumeration values = req.getParameterNames();
while(values.hasMoreElements()) {
String name = (String)values.nextElement();
String value = req.getParameterValues(name)[0];

if(name.compareTo("submit") != 0) {
toFile.println(name + ": " + value);


// Respond to client with a thank you
toClient.println("<title>Thank you!</title>");
toClient.println("<body bgcolor=#ffffff><font face=\"Helvetica\">" +
"Thank you for participating</font>");

try {
BufferedReader inn = new BufferedReader(
new FileReader("myfile"));
String str;
while ((str = inn.readLine()) != null) {
} catch (IOException e) {


} catch(IOException e) {
toClient.println("A problem occured while recording your answers. " +
"Please try again.");

Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Hi, I think you should take a look at RandomAccesFile class, than you�ll be able to append your files.
Have you checked out Aspose?
subject: saving data and appending to file
jQuery in Action, 3rd edition