Unravel โบ Java concepts โบ I/O and files
Properties files, Locale and resource bundles
Properties loads key=value text files into a map.
Learn these first
At a glance
Properties is a settings notebook with one setting per line.
- 1Properties p = new Properties(); p.load(in); then p.getProperty("timeout", "30") with a default valueValues are always strings; convert them yourself.
- 2ResourceBundle.getBundle("messages", Locale.GERMAN).getString("welcome") reads messages_de.properties, falling back to messages.properties
- 3MessageFormat.format("Hello {0}, you have {1} new messages", name, n) fills numbered placeholders, and handles plural choices with a pattern
- 4A Locale controls number and date formatting (1.234,56 vs 1,234.56), sorting order and case rules
- .properties files were read as ISO-8859-1 before Java 9; since then UTF-8 is the default for resource bundles
- Always pass a Locale to formatting for machine-readable output (Locale.ROOT or Locale.US); the default locale makes tests pass on your laptop and fail on the CI server
- Properties extends Hashtable<Object,Object>, so it is synchronized and accepts non-String values, which is a design wart
import java.io.*;import java.util.*;public class Config { static Properties load(String path) throws IOException { Properties p = new Properties(); try (Reader r = new FileReader(path, java.nio.charset.StandardCharsets.UTF_8)) { p.load(r); } return p; } static int timeout(Properties p) { return Integer.parseInt(p.getProperty("timeout", "30")); } static String greet(String name) { return ResourceBundle.getBundle("messages", Locale.GERMAN).getString("hello") + " " + name; }}- line 4 uses Properties, the core of Properties files, Locale and resource bundles
- line 5 uses Properties, the core of Properties files, Locale and resource bundles
- line 9 uses Properties, the core of Properties files, Locale and resource bundles
- line 10 uses ResourceBundle, the core of Properties files, Locale and resource bundles
Read the full explanation (about 1 min)
Properties loads key=value text files into a map. Locale and ResourceBundle pick language-specific texts, number formats and date formats for the user's region.
A mental model
Properties is a settings notebook with one setting per line. A ResourceBundle is a set of phrasebooks (English, German, French) and the Locale is the page marker telling the program which one to open.
How it works
Properties p = new Properties(); p.load(in); then p.getProperty("timeout", "30") with a default value. Values are always strings; convert them yourself.
ResourceBundle.getBundle("messages", Locale.GERMAN).getString("welcome") reads messages_de.properties, falling back to messages.properties.
MessageFormat.format("Hello {0}, you have {1} new messages", name, n) fills numbered placeholders, and handles plural choices with a pattern.
A Locale controls number and date formatting (1.234,56 vs 1,234.56), sorting order and case rules.
Going deeper
.properties files were read as ISO-8859-1 before Java 9; since then UTF-8 is the default for resource bundles. Load a plain Properties through a Reader with an explicit charset.
Always pass a Locale to formatting for machine-readable output (Locale.ROOT or Locale.US); the default locale makes tests pass on your laptop and fail on the CI server.
Spring Boot replaces most hand-written Properties code with @ConfigurationProperties, but plain Properties still appears in JDBC, Kafka and many older libraries.
Under the hood
Properties extends Hashtable<Object,Object>, so it is synchronized and accepts non-String values, which is a design wart. ResourceBundle.getBundle caches bundles per class loader and walks a fallback chain of candidate names.
Common mistakes
- Forgetting that every value is a String.
- Relying on the default Locale for output that is parsed again.
- Opening the properties file with the wrong class loader or path.
How Unravel recognises it
When you paste code, Unravel looks for these signs of properties files, locale and resource bundles. Spotting them yourself is a good habit when you read unfamiliar code:
- Types:
PropertiesResourceBundleLocaleMessageFormatListResourceBundle - Method calls:
getProperty()setProperty()Locale.forLanguageTag()
Part of a learning path
- I/O, networking and time ยท previous: Gson and other JSON APIs ยท next: Formatting, parsing and date arithmetic
Related concepts
Frequently asked questions
What is Properties files, Locale and resource bundles?
Properties loads key=value text files into a map. Locale and ResourceBundle pick language-specific texts, number formats and date formats for the user's region.
What are the most common mistakes with Properties files, Locale and resource bundles?
Forgetting that every value is a String. Relying on the default Locale for output that is parsed again. Opening the properties file with the wrong class loader or path.
How can I see Properties files, Locale and resource bundles in real code?
Paste your own Java into Unravel and it will point out where properties files, locale and resource bundles appears, explain how it works at the detail level you choose, and draw the flow of the code around it.