关于修改properties文件求指点

狂奔蜗牛 发布于 2013/01/22 18:05
阅读 1K+
收藏 0
properties=new Properties();
try{
InputStream inputFile=LoginAction.class.getResourceAsStream("/JUP.properties");
properties.load(inputFile);
PrintWriter outputFile=new PrintWriter(new BufferedWriter(new FileWriter("/JUP.properties")));
if(properties.getProperty("LZPHONE_PWD").equals(loginPwd)){
properties.setProperty("LZPHONE_PWD", lastPwd);
outputFile.flush();
properties.list(outputFile);
//properties.store( outputFile);

outputFile.close();
outjson("{\"msg\":\"密码修改成功!\",\"success\":true}");
System.out.print("密码修改成功");
}else{
outjson("{\"msg\":\"输入的旧密码错误!\",\"success\":false}");
System.out.println("输入的旧密码错误");
//提示旧密码错误
}
}catch(FileNotFoundException ex){
ex.printStackTrace();
outjson("{\"msg\":\""+ex.getMessage()+"\",\"success\":false}");
}catch(IOException e){
e.printStackTrace();
outjson("{\"msg\":\""+e.getMessage()+"\",\"success\":false}");

}

我想改变JUP.properties文件中LZPHONE_PWD的值,但是每次去查文件中的值一直没有改变

store()也不行,大神帮忙看看


加载中
0
天马行空&
天马行空&

如果你要随时获取修改过.properties文件内容,不要使用InputStream inputFile=LoginAction.class.getResourceAsStream("/JUP.properties"); 这种方式获取,它会在类的缓存读取已经加载的文件。你修改过也没有用。

public static  String getLabel(String propertyName, String label, String defaultVal){
	     Properties properties = new Properties();
	    try {
	        InputStream in =new BufferedInputStream(new FileInputStream(Constants.ClASSPATH+File.separator+propertyName));
	        properties.load(in);
	        in.close();
	      } catch (IOException e) {
	        e.printStackTrace();
	     }
	     return properties.getProperty(label, defaultVal);
	    }
Constants.ClASSPATH 这个常量你可以项目初化时赋值。

String rootpath = event.getServletContext().getRealPath("/");

 Constants.ClASSPATH = rootpath + "WEB-INF/classes/";

packagecom.gpcsoft.core.web.listen;

importcom.gpcsoft.core.Constants;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.List;
importjava.util.Map;
importjava.util.Properties;
importjavax.servlet.ServletContext;
importjavax.servlet.ServletContextEvent;
importjavax.servlet.ServletContextListener;
importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.springframework.beans.factory.NoSuchBeanDefinitionException;
importorg.springframework.context.ApplicationContext;
importorg.springframework.security.providers.AuthenticationProvider;
importorg.springframework.security.providers.ProviderManager;
importorg.springframework.security.providers.encoding.Md5PasswordEncoder;
importorg.springframework.security.providers.rememberme.RememberMeAuthenticationProvider;
importorg.springframework.web.context.ContextLoaderListener;
importorg.springframework.web.context.support.WebApplicationContextUtils;

publicclassStartupListenerextendsContextLoaderListener
implementsServletContextListener
{
privatestaticfinalLoglog=LogFactory.getLog(StartupListener.class);

publicvoidcontextInitialized(ServletContextEventevent){
	if(log.isDebugEnabled()){
		log.debug("initializingcontext...");
	}
	super.contextInitialized(event);
	ServletContextcontext=event.getServletContext();
	Mapconfig=(HashMap)context.getAttribute("appConfig");
	if(config==null){
		config=newHashMap();
	}
	if(context.getInitParameter("csstheme")!=null){
		config.put("csstheme",context.getInitParameter("csstheme"));
	}
	ApplicationContextctx=
	WebApplicationContextUtils.getRequiredWebApplicationContext(context);
	Constants.ctx=ctx;
	Stringrootpath=event.getServletContext().getRealPath("/");
	if(rootpath!=null)
	{
		rootpath=rootpath.replaceAll("\\\\","/");
	}
	else
	{
		rootpath="/";
	}

	if(!(rootpath.endsWith("/")))
	{
		rootpath=rootpath+"/";
	}

	Constants.ROOTPATH=rootpath;

	Propertiesprop=newProperties();
	InputStreamstream=super.getClass().getResourceAsStream("/configpath.properties");
	try{
		prop.load(stream);
		Stringconfig_path=prop.getProperty("config_path");
		if(!(config_path.endsWith("/"))){
			config_path=config_path+"/";
		}
		if((config_path.equals("/"))||(config_path.equals("/WEB-INF/classes/"))){
			Constants.CONFIGPATH=rootpath+"WEB-INF/classes/";
		}
		else{
			Constants.CONFIGPATH=config_path;
		}
		log.info("Constants.CONFIGPATH:::::::::::::::::;"+Constants.CONFIGPATH);
	}
	catch(IOExceptionex){
		log.error(ex);
		Constants.CONFIGPATH=rootpath+"WEB-INF/classes/";
	}
		log.info("ApplicationRunConfigPath:"+Constants.CONFIGPATH);
		Constants.ClASSPATH=rootpath+"WEB-INF/classes/";
		Constants.LIBPATH=rootpath+"WEB-INF/lib/";
	}
}

返回顶部
顶部