Java 如何在liferay中保存属性文件
我需要以编程方式更改属性,以保持服务器重新启动、重新部署等。基本上,当进行更改时,我需要修改属性文件。这可能吗?目前,我的属性正在工作,但属性文件夹中的值从未更改Java 如何在liferay中保存属性文件,java,liferay,Java,Liferay,我需要以编程方式更改属性,以保持服务器重新启动、重新部署等。基本上,当进行更改时,我需要修改属性文件。这可能吗?目前,我的属性正在工作,但属性文件夹中的值从未更改 String realPath = getPortletContext().getRealPath("/"); FileInputStream in = new FileInputStream(realPath+"json.properties"); Propertie
String realPath = getPortletContext().getRealPath("/");
FileInputStream in = new FileInputStream(realPath+"json.properties");
Properties p = new Properties();
p.load(in);
System.out.println(p.getProperty("json"));
in.close();
p.setProperty("json", "test");
System.out.println(p.getProperty("json"));
FileOutputStream out = new FileOutputStream(realPath+"json.properties");
p.store(out, "test");
in.close();
out.close();
System.out.println(com.liferay.util.portlet.PortletProps.get("json"));
com.liferay.util.portlet.PortletProps.set("json", "change");
com.liferay.util.portlet.PortletProps.set("new", "change");
System.out.println(com.liferay.util.portlet.PortletProps.get("new"));
portal.properties:
json=help
new=new1
json.properties
json=blank
运行代码时的输出:
空白
测试
帮助
改变
正如您在上面看到的,我已经尝试使用常规java属性,并尝试使用liferay属性。我需要基本上需要得到一些json,编辑它,然后保存它供以后使用。我需要这些更改保持到再次编辑 我以前所有的代码都是垃圾,就像我解决它时使用的那样
PortletPreferences portletPreference = actionRequest.getPreferences();
portletPreference.setValue("help", "me");
portletPreference.store();
注意:我以前试过,但运气不好,问题是我的进口。确保您使用
javax.portlet.PortletPreferences;而不是liferay portletpreferences类 当然这是可能的:任何程序都可以写入和读取文件。但是,如果您告诉我们这将为您解决什么问题,我们可能会给您一个提示,这是否是一种良好的做法,或者您是否可以通过PortletPreferences更轻松地解决您的问题(例如,正如您在当前可用的一个答案中所述)。(当然,还有其他可能性)