How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray?

A Properties object can hold Objects, not just Strings. That tends to be forgotten because they’re overwhelmingly used to load .properties files, and so often will only contain Strings. The documentation indicates that calling bundle.getStringArray(key) is equivalent to calling (String[]) bundle.getObject(key). That’s the problem: the value isn’t a String[], it’s a String. I’d suggest storing … Read more

How to load a resource bundle from a file resource in Java?

As long as you name your resource bundle files correctly (with a .properties extension), then this works: File file = new File(“C:\\temp”); URL[] urls = {file.toURI().toURL()}; ClassLoader loader = new URLClassLoader(urls); ResourceBundle rb = ResourceBundle.getBundle(“myResource”, Locale.getDefault(), loader); where “c:\temp” is the external folder (NOT on the classpath) holding the property files, and “myResource” relates to … Read more

Resource files not found from JUnit test cases

My mistake, the resource files WERE actually copied to target/test-classes. The problem seemed to be due to spaces in my project name, e.g. Project%20Name. I’m now loading the file as follows and it works: org.apache.commons.io.FileUtils.toFile(myClass().getResource(“resourceFile.txt”)‌​); Or, (taken from Java: how to get a File from an escaped URL?) this may be better (no dependency on … Read more

Format a message using MessageFormat.format() in Java

Add an extra apostrophe ‘ to the MessageFormat pattern String to ensure the ‘ character is displayed String text = java.text.MessageFormat.format(“You”re about to delete {0} rows.”, 5); ^ An apostrophe (aka single quote) in a MessageFormat pattern starts a quoted string and is not interpreted on its own. From the javadoc A single quote itself … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)