java 获取文件内容并放入内存

Spring boot项目,项目启动会自动跑该代码

@Component
public class MemoryConfig {
	
	
	public Map<String,String> data = new HashMap<>();
	
	@Value("${Sourcepath.config}")
    String Sourcepath;
	
	@Bean
	public void addMemory() throws Exception {
		//
		String[] codes = {"xQCC001","xQCC002","xQCC003","xQCC004"};
		for(String code : codes) {
			List<String> fileNames = getList(Sourcepath + File.separator + code);
			for(String fileName : fileNames ) {
				StringBuilder value = new StringBuilder();
				
				String path = Sourcepath + File.separator + code + File.separator + fileName;
				File jsonFile = new File(path);
		        
		        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile), "UTF-8"));
		        String str = null;
		        while((str = br.readLine()) != null){
		        	value.append(str);
		        }
		        data.put(fileName, value.toString());
			}
		}
	}
	
	public List<String> getList(String path) throws Exception{
		List<String> result = new ArrayList<>();
		File fileList = new File(path);
		for(File file1:fileList.listFiles()) {
			result.add(file1.getName());
		}
		return result;
	}
}

你可能感兴趣的:(Java基础,1024程序员节,java,spring,maven,junit)