I have a servlet in which the from InputStream I am getting the my form data in XML format. I am able to get retrieve the form data in XML format and able to write the same in file. If I open the file I am able to see my form data.
Now the issue is, When i try to append the form data to the string buffer it is not happening. I tried buffer.append(). After that method When I try to print the string buffer value nothing is showing/printing in the console.
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("html/text");
PrintWriter out = response.getWriter();
out.println("doPost Method is excecuting");
DataInputStream in = new DataInputStream (request.getInputStream());
StringBuffer buffer = new StringBuffer();
File file = new File("reqOutput.txt");
file.createNewFile();
FileWriter writer = new FileWriter(file);
int value;
while ((value=in.read()) != -1) {
buffer.append(value);
writer.write(value);
}
System.out.println("Value is : "+ buffer.toString()); // Nothing is printing
writer.flush();
writer.close();
}
What's wrong with my code.Any suggestions please.
-
1The while loop wouldn't have got executed. there is no problem with StringBufferKalaiarasan Manimaran– Kalaiarasan Manimaran2015年02月10日 09:49:14 +00:00Commented Feb 10, 2015 at 9:49
-
1Use StringBuilder instead of StringBuffer.Adriaan Koster– Adriaan Koster2015年02月10日 09:50:00 +00:00Commented Feb 10, 2015 at 9:50
-
is your 'in.read()' returning correct values?Vihar– Vihar2015年02月10日 09:50:53 +00:00Commented Feb 10, 2015 at 9:50
-
Have you checked the console. it should print if it goes inside the loop and your file has contents?. check by changing outputfilename. might be you are checking old file i guessKalaiarasan Manimaran– Kalaiarasan Manimaran2015年02月10日 09:51:39 +00:00Commented Feb 10, 2015 at 9:51
-
Yes the read() is returning the correct values, I am writing the form data in to a file in in While loop only. The file is created and appropriate dataVinod– Vinod2015年02月10日 09:53:13 +00:00Commented Feb 10, 2015 at 9:53
1 Answer 1
Here is your code modified to read from a file:
public static void main(final String[] args) throws Exception {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("test"));
final StringBuilder sb = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
System.out.println("Value is : " + sb.toString());
} finally {
if (in != null) {
in.close();
}
}
}
I added a BufferedReader around the FileReader to optimize the reading. I switched from reading one character at a time to reading line by line. This also gives you the results as String so you don't have to convert the int.
Furthermore I added resource handling (pre-7 style and without exception handling) and switched to StringBuilder.
Output:
hello world! -> Value is : hello world!
I think there is another problem, not in this part of your code.
Additional comments on your code (not related to the question):
StringBuffer is a thread-safe implementation. If you have no need for this (like in your servlet example) you'd better use StringBuilder.
Don't close resources within the code block, use a try-finally (or since Java 7 try-with-resources) to guarantee resources are always closed, even when exceptions occur in the block somewhere.