0

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.

6
  • 1
    The while loop wouldn't have got executed. there is no problem with StringBuffer Commented Feb 10, 2015 at 9:49
  • 1
    Use StringBuilder instead of StringBuffer. Commented Feb 10, 2015 at 9:50
  • is your 'in.read()' returning correct values? Commented 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 guess Commented 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 data Commented Feb 10, 2015 at 9:53

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there chance to get the value in string format instead integer values?
Using StringBuilder over StringBuffer won't solve any problem here. I know there would be a very minuscule speed gain by doing so (that will be far outweighed by the sedentary pace of both the web service and the file operations), but it's pretty irrelevant to this question. I'm voting you up because your main point ("the problem is elsewhere") is sound.
It is irrelevant, but still good practice. See also the comment I just added about resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.