读取某个目录的文件属性文件
ResourcePatternResolver rer = new PathMatchingResourcePatternResolver();
?
Resource[] resources = (Resource[])null;
?? ?try {
?? ? ?resources = rer.getResources(“classpath*:/config/**/*.properties”);
?? ?} catch (IOException ioe) {
?? ? ?log.error("读取资源文件时出现错误,返回null ...", ioe);
?? ? ?return null;
?? ?}
?
?
for (Resource res : resources) {
?? ? ?try {
?? ? ? ?InputStream is = null;
?? ? ? ?URL resourceUrl = res.getURL();
?? ? ? ?String fileName = res.getFilename();
?? ? ? ?if ("file".equals(resourceUrl.getProtocol())) {
?
?? ? ? ? ?is = new FileInputStream(res.getFile());
?? ? ? ? ?if (fileName.endsWith(".properties")) {
?? ? ? ? ? ?this.load(is);
?? ? ? ? ?}
?? ? ? ? ?p.subProperties.add(subProperty);
?? ? ? ? ?log.debug("成功从文件系统加载资源文件[" + subProperty.path + "]..."); break label372:
?? ? ? ?}
?? ? ? ?log.debug("资源文件[" + fileName + "]不存在于文件系统,将通过ClassLoader进行加载,热修改功能关闭...");
?? ? ? ?is = res.getInputStream();
?? ? ? ?if (fileName.endsWith(".xml")) {
?? ? ? ? ?p.loadFromXml(is); break label372: }
?? ? ? ?if (fileName.endsWith(".properties")) {
?? ? ? ? ?p.load(is);
?? ? ? ?}
?? ? ?}
?? ? ?catch (Exception e)
?? ? ?{
?? ? ? ?log.error("加载属性文件时出现错误 ... ", e);
?? ? ?}
?? ?}
?
//加载
?
public void load(InputStream inStream) throws IOException
??{
?? ?super.load(inStream);
?? ?String encoding = super.getProperty("ENCODING");
?? ?if ("ISO-8859-1".equals(encoding)) {
?? ? ?return;
?? ?}
?? ?try
?? ?{
?? ? ?Map.Entry[] entries = new Map.Entry[entrySet().size()];
?? ? ?entrySet().toArray(entries);
?? ? ?for (int i = entries.length; --i >= 0; ) {
?? ? ? ?Map.Entry entry = entries[i];
?? ? ? ?String value = (String)entry.getValue();
?? ? ? ?value = new String(value.getBytes("ISO-8859-1"), encoding);
?? ? ? ?String key = new String(((String)entry.getKey()).getBytes("ISO-8859-1"), encoding);
?? ? ? ?setProperty(key, value);
?? ? ?}
?? ?}
?? ?catch (UnsupportedEncodingException localUnsupportedEncodingException)
?? ?{
?? ?}
??}
?
?