我有一個Servlet,其中InputStream是以XML格式獲取我的表單數據。我能夠以XML格式檢索表單數據並能夠在文件中寫入相同的數據。如果我打開文件,我可以看到我的表單數據。java中的字符串緩衝區問題
現在的問題是,當我嘗試追加表單數據到字符串緩衝區它不會發生。我試過buffer.append()
。在該方法之後當我嘗試打印字符串緩衝區值時,控制檯中沒有顯示/打印任何內容。
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();
}
我的代碼有什麼問題,請提出任何建議。
while循環不會被執行。 StringBuffer沒有問題 – 2015-02-10 09:49:14
使用StringBuilder代替StringBuffer。 – 2015-02-10 09:50:00
是你的'in.read()'返回正確的值嗎? – Vihar 2015-02-10 09:50:53