-2

I am having trouble while writing to file using the PrintWriter. Following is my code:

String abc = request.getParameter("textAreaField"); //String is "a b c" (with spaces)
String fileA = dir + "/A";
PrintWriter fileWriterA = new PrintWriter(new FileOutputStream(fileA,true));
fileWriterA.println(abc);
fileWriterA.close();

The problem I am having here is while writing to the file "A" in the directory "dir" only "a" from String abc will be written and the rest after the space is not written. String abc here in the code is coming from a textarea in html and I have the above code in my servlet. I am not able to understand why it won't write the string with spaces to file. I think it should. I have also checked by printing the String abc and it does print the string "a b c" (with spaces). But it won't print that to file. Is there a problem with my code? Any help would be appreciated.

Thanks in advance.

Hemang
  • 390
  • 3
  • 20
  • You aren't transmitting it properly from the text area *to* the Servlet. The idea that `PrintWriter` is responsible for this is ridiculous. – user207421 Dec 09 '14 at 08:52
  • Have you checked, if abc contains the expected string? I have tried it with a fix string and it works fine. – Jens Dec 09 '14 at 08:53
  • @EJP: I never blamed the PrintWriter for not writing it correctly. If you notice I have said "Is there a problem with my code?". Also regarding whether I have checked my string whether it is transmitting correctly, that is also there in my question "I have also checked by printing the String abc and it does print the string "a b c" (with spaces)." – Hemang Dec 09 '14 at 09:00
  • @Hernang That is exactly what is claimed in your title. If it doesn't reflect your actual views, edit it. – user207421 Dec 09 '14 at 09:24
  • You're still wrong. The problem, if any, is clearly present in the variable `abc` before it gets written. It's absurd to blame this on the writing process in any way. It's not the business of I/O to change the data, and it doesn't. – user207421 Dec 09 '14 at 22:10

1 Answers1

0

I have used your code and written a servlet . It is working absolutely fine. Here is the code.

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {
    System.out.println(request.getParameter("ta"));
    String abc = request.getParameter("ta");
    String fileA = "/A";
    PrintWriter fileWriterA = new PrintWriter(new FileOutputStream(fileA,true));
    fileWriterA.println(abc);
    fileWriterA.close();
}

and here is the jsp:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Test">
<textarea rows="20" cols="20" name="ta"></textarea><!-- having value -- check some spaces -->
<input type="submit" value="Submit">
</form>
</body>
</html>
kenju
  • 5,866
  • 1
  • 41
  • 41
Vishu
  • 29
  • 10