public static Map<String, String> getResourceBundle() throws IOException {…
- User
public static Map<String, String> getResourceBundle() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); List resourcePaths = new ArrayList<>(); Resource[] resources = resolver.getResources("classpath*:**/*"); for (Resource resource : resources) { if (resource.isReadable()) { String path = resource.getURL().getFile(); if ( path.endsWith("messages.properties")){ resourcePaths.add(path); }
} } // todo extract a HashMap with <key - value> from the properties file } - Assistant
Here's how you can complete the method to extract key-value pairs from the properties files:
public static Map<String, String> getResourceBundle() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); List<String> resourcePaths = new ArrayList<>(); Resource[] resources = resolver.getResources("classpath*:**/*"); for (Resource resource : resources) { if (resource.isReadable()) { String path = resource.getURL().getFile(); if (path.endsWith("messages.properties")) { resourcePaths.add(path); } } } // Extract HashMap with <key - value> from the properties files Map<String, String> allProperties = new HashMap<>(); for (String resourcePath : resourcePaths) { Properties props = new Properties(); try (InputStream inputStream = new FileInputStream(resourcePath)) { props.load(inputStream); // Convert Properties to Map<String, String> for (String key : props.stringPropertyNames()) { allProperties.put(key, props.getProperty(key)); } } catch (IOException e) { // Log the error or handle as needed System.err.println("Error loading properties from: " + resourcePath); throw e; } } return allProperties; }However, there's a more efficient approach using Spring's Resource objects directly instead of file paths:
public static Map<String, String> getResourceBundle() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); Resource[] resources = resolver.getResources("classpath*:**/messages.properties"); Map<String, String> allProperties = new HashMap<>(); for (Resource resource : resources) { if (resource.isReadable()) { Properties props = new Properties(); try (InputStream inputStream = resource.getInputStream()) { props.load(inputStream); // Convert Properties to Map<String, String> for (String key : props.stringPropertyNames()) { allProperties.put(key, props.getProperty(key)); } } catch (IOException e) { System.err.println("Error loading properties from: " + resource.getDescription()); throw e; } } } return allProperties; }The second approach is better because:
- It uses the resource pattern directly to find
messages.propertiesfiles - It uses
resource.getInputStream()which works reliably with JAR files and other resource locations - It avoids potential issues with file path handling across different environments
Both versions will merge all properties from all found
messages.propertiesfiles into a single map. If there are duplicate keys across files, the last one processed will win. - It uses the resource pattern directly to find
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.