Search code examples
javainputstreamstringbuffer

Issue with String Buffer in java


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.


Solution

  • 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.