当前位置: 代码迷 >> Java Web开发 >> properties文件中的数剧无法更改的有关问题
  详细解决方案

properties文件中的数剧无法更改的有关问题

热度:2747   发布时间:2013-02-25 21:13:59.0
properties文件中的数剧无法更改的问题
public class CountServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("image/jpeg");
  ServletOutputStream sos = response.getOutputStream();
BufferedImage image = new BufferedImage(80, 20,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.red);
g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));
String value=getAccessCount(request);
g.drawString(value,0,18);
g.dispose();
ImageIO.write(image, "JPEG", sos);
sos.close();
}

private String getAccessCount(HttpServletRequest request) {
String Pagekey=null;
Properties proty = new Properties();
String count = "0";
try {
InputStream input=this.getClass().getResourceAsStream("/count.properties");
proty.load(input);
count = proty.getProperty("Pagekey");
int c = Integer.parseInt(count)+ 1;
count = new Integer(c).toString();
proty.put("Pagekey", "count");
proty.store(new FileOutputStream(this.getServletContext().getRealPath("/count.properties")), "the page is accessed");
input.close();
} catch (IOException e) {

e.printStackTrace();
}

return count;
}

}

上面的程序是统计访问页面的人数,统计的数据保存在count.properties中,属性文件内容Pagekey=15,但我第一次访问后,浏览器中显示为16,但是属性文件数据没变化,这儿的store()方法没起作用吗?该怎么改??


------解决方案--------------------------------------------------------
流没关闭。

proty.store(new FileOutputStream(this.getServletContext().getRealPath("/count.properties")), "the page is accessed");

==>
FileOutputStream fos = new FileOutputStream(this.getServletContext().getRealPath("/count.properties"))
proty.store(fos , "the page is accessed");
fos.close();
  相关解决方案