• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

saving data and appending to file

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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 code..so 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
{
res.setContentType("text/html");
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);


toFile.println("<BEGIN>");
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);
}
}

toFile.println("<END>");

bf.close();
in.close();
// Respond to client with a thank you
toClient.println("<html>");
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) {
toClient.println(str);
toClient.println("<br>");
}
inn.close();
} catch (IOException e) {
}
toClient.println("</body>");

toClient.println("</html>");

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

}
}
 
Ranch Hand
Posts: 977
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I think you should take a look at RandomAccesFile class, than you�ll be able to append your files.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic