Code example for StringWriter

Methods: flushtoString

0
        try { 
            JSONObject o = new JSONObject();
            o.put("code", "error");
            o.put("message", e.getMessage());
     
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            pw.flush();
            sw.flush();
     
            o.put("stack", sw.toString());
     
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Type", "application/json");
            respond(response, o.toString());
        } catch (JSONException e1) {
            e.printStackTrace(response.getWriter());
        } 
    } 
Experience pair programming with AI  Get Codota for Java