• Post Reply Bookmark Topic Watch Topic
  • New Topic

saving more than one value in a file  RSS feed

kalyani anne
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am really confused.I thought it's simple..but some where it's going wrong.
i am trying to get data from browser and save it in a file.but the file is not created.i am giving the code below.
can any one help me please
thanks in advance
public class servlet1 extends HttpServlet
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
try {
String surveyName = req.getParameterValues("servlet1")[0];
FileWriter resultsFile = new FileWriter(System.getProperty("file.separator")+ surveyName + ".txt", true);
PrintWriter toFile = new PrintWriter(resultsFile);
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);
} catch(IOException e) {
toClient.println("A problem occured while recording your answers. " +
"Please try again.");
Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file isn't being created at all, then look carefully at the code that creates the FileWriter. Try splitting this up into several steps, and print out some diagnostic info along the way:
<code><pre> String surveyName = req.getParameterValues("servlet1")[0];
System.out.println("Survey name is " + surveyName);
File resultsFile = new File(System.getProperty("file.separator")+ surveyName + ".txt";
System.out.println("Results file is " + resultsFile);
System.out.println(" absolute path is " + resultsFile.getAbsolutePath());
FileWriter resultsWriter = new FileWriter(resultsFile, true);
PrintWriter toFile = new PrintWriter(resultsWriter);
Is the file name generated what you expected? Does the absolute path point to the directory you want? This should give you some good clues what's going on.
[This message has been edited by Jim Yingst (edited July 24, 2001).]
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!