Skip to content
TheChatArchiveThe Chat Archive
Qwenqwen3-235b-a22b-no-thinking· Alibaba (Qwen)Hugging Face LMArena Human Preference 140K

public static Map<String, String> getResourceBundle() throws IOException {…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Claudeclaude-sonnet-4-20250514’s answer.
  1. 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
    
    }
    
  2. Qwen Assistant

    To complete the method getResourceBundle() and extract a Map<String, String> containing key-value pairs from all the messages.properties files found in the classpath, you can proceed as follows:

    ✅ Steps:

    1. Iterate over each properties file found (messages.properties).
    2. Load each file using Properties.
    3. Add each key-value pair into a resulting map.
    4. Return the final map.

    Here's the completed version of your method:

    import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URLDecoder;
    import java.nio.charset.StandardCharsets;
    import java.util.*;
    
    public class ResourceBundleLoader {
    
        public static Map<String, String> getResourceBundle() throws IOException {
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            Map<String, String> propertyMap = new HashMap<>();
    
            // Find all resources in the classpath
            Resource[] resources = resolver.getResources("classpath*:**/*");
    
            for (Resource resource : resources) {
                if (resource.isReadable()) {
                    String path = resource.getURL().getFile();
    
                    // Decode URL-encoded paths (e.g., in JARs)
                    path = URLDecoder.decode(path, StandardCharsets.UTF_8.name());
    
                    if (path.endsWith("messages.properties")) {
                        try (InputStream is = resource.getInputStream()) {
                            Properties props = new Properties();
                            props.load(is);
    
                            for (String key : props.stringPropertyNames()) {
                                String value = props.getProperty(key);
                                propertyMap.put(key, value);
                            }
                        }
                    }
                }
            }
    
            return propertyMap;
        }
    }
    

    🔍 Notes:

    • URLDecoder.decode(...) is used to decode paths like those inside JAR files which are often URL-encoded.
    • The method uses try-with-resources to ensure the InputStream is closed properly after reading.
    • This approach assumes that keys are unique across all messages.properties files. If duplicates exist, the last one loaded will overwrite earlier ones. You can modify this behavior if needed (e.g., skip or merge).

    🧪 Example Usage:

    public static void main(String[] args) {
        try {
            Map<String, String> bundle = getResourceBundle();
            bundle.forEach((key, value) -> System.out.println(key + " = " + value));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    Let me know if you want to also support locale-specific bundles like messages_en_US.properties, or merge values instead of overwrite!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.