robin_77
偷懒是一种必备素质

一、读取config和jar里的配置文件,以流的方式返回 

 1 /**
 2      * 根据路径,获取当前项目中路径(jar包和config文件)下的配置文件
 3      * @param filePath 存放job配置信息文件的相对路径
 4      * @return job.xml文件流
 5      */
 6     private static List<InputStream> getJobFilesInputStream(String filePath) throws IOException {
 7         List<InputStream> jobFilesInputStreams = new ArrayList<>();
 8         ResourcePatternResolver resolver =  new PathMatchingResourcePatternResolver();
 9         Resource[] resources = resolver.getResources("classpath*:"+ filePath +"/*.xml");
10         Map<String,String> fileMap = new HashMap<>();
11         for (Resource resource:resources){
12             String key = resource.getFilename();
13             if(StringUtils.isBlank(fileMap.get(key))){//检查是否已经读取同文件的流
14                 jobFilesInputStreams.add(resource.getInputStream());
15                 fileMap.put(key,key);
16             }
17         }
18         return jobFilesInputStreams;
19     }

二、读取config和jar里的配置文件,以文件对象的方式返回

 1 /**
 2      * 根据路径,获取当前项目中路径(jar包和config文件)下的配置文件
 3      * @param filePath 存放job配置信息文件的相对路径
 4      * @return job.xml文件对象
 5      */
 6     private static List<File> getJobFiles(String filePath) throws IOException {
 7         List<File> jobFiles = new ArrayList<>();
 8         ResourcePatternResolver resolver =  new PathMatchingResourcePatternResolver();
 9         Resource[] resources = resolver.getResources("classpath*:"+ filePath +"/*.xml");
10         Map<String,String> fileMap = new HashMap<>();
11         for (Resource resource:resources){
12             String key = resource.getFilename();
13             if(StringUtils.isBlank(fileMap.get(key))){//检查是否已经读取同文件的流
14                 jobFiles.add(resource.getFile());
15                 fileMap.put(key,key);
16             }
17         }
18         return jobFiles;
19     }

三、读取config和jar里的配置文件,以文件路径的方式返回

 1 /**
 2      * 根据路径,获取当前项目中路径(jar包和config文件)下的配置文件
 3      * @param filePath 存放job配置信息文件的相对路径
 4      * @return job.xml文件路径
 5      */
 6     private static List<String> getJobFilesPath(String filePath) throws IOException {
 7         List<String> jobFilesPath = new ArrayList<>();
 8         ResourcePatternResolver resolver =  new PathMatchingResourcePatternResolver();
 9         Resource[] resources = resolver.getResources("classpath*:"+ filePath +"/*.xml");
10         Map<String,String> fileMap = new HashMap<>();
11         for (Resource resource:resources){
12             String key = resource.getFilename();
13             if(StringUtils.isBlank(fileMap.get(key))){//检查是否已经读取同文件的流
14                 jobFilesPath.add(resource.getFile().getPath());
15                 fileMap.put(key,key);
16             }
17         }
18         return jobFilesPath;
19     }

 

经测试:三种方式,都能读取到文件,无论config文件夹下还是jar包里的配置文件。但是因为其他工具类需要配合使用这些配置文件,第二种和第三种不支持配置文件直接打入jar包,因此第一种是比较通用的做法。

第一种有一个地方需要特别注意:流数据只能被消费一次,消费一次后,流中的数据就没有了。

解决方案:成员变量,接收流数据写入内存,每次要用的时候,将这个成员变量数据再转换成流,供其他调用的工具方法进行消费。

posted on 2016-07-01 10:24  robin_77  阅读(277)  评论(0编辑  收藏  举报