Answer for “pre-Java-9” is below. As of Java 9, properties files are saved and loaded in UTF-8 by default, but falling back to ISO-8859-1 if an invalid UTF-8 byte sequence is detected. See the Java 9 release notes for details.
Properties files are ISO-8859-1 by definition – see the docs for the Properties class.
Spring has a replacement which can load with a specified encoding, using PropertiesFactoryBean
.
EDIT: As Laurence noted in the comments, Java 1.6 introduced overloads for load
and store
which take a Reader
/Writer
. This means you can create a reader for the file with whatever encoding you want, and pass it to load
. Unfortunately FileReader
still doesn’t let you specify the encoding in the constructor (aargh) so you’ll be stuck with chaining FileInputStream
and InputStreamReader
together. However, it’ll work.
For example, to read a file using UTF-8:
Properties properties = new Properties();
InputStream inputStream = new FileInputStream("path/to/file");
try {
Reader reader = new InputStreamReader(inputStream, "UTF-8");
try {
properties.load(reader);
} finally {
reader.close();
}
} finally {
inputStream.close();
}