public final class PropertiesMessageSource extends Object implements MessageSource
You can load property files from the classpath or from files on your filesystem. You can also specify the character set used to read the file; methods without a character set as an argument use UTF-8.
Properties.load(Reader)
Modifier and Type | Method and Description |
---|---|
static MessageSource |
fromFile(File file)
Create a message source from a properties file on the filesystem using
UTF-8
|
static MessageSource |
fromFile(File file,
Charset charset)
Create a message source from a properties file on the filesystem using
the specified charset
|
static MessageSource |
fromPath(String path)
Create a message source from a properties file on the filesystem using
the specified encoding
|
static MessageSource |
fromPath(String path,
Charset charset)
Create a message source from a properties file on the filesystem using
the specified charset
|
static MessageSource |
fromResource(String resourcePath)
Create a message source from a classpath resource using UTF-8
|
static MessageSource |
fromResource(String resourcePath,
Charset charset)
Create a message source from a classpath resource using the specified
charset
|
String |
getKey(String key)
Return a message matching a given key
|
public static MessageSource fromResource(String resourcePath) throws IOException
resourcePath
- the path to the properties fileNullPointerException
- resource path is nullIOException
- no such resource, or an I/O error occurred while
reading the filepublic static MessageSource fromResource(String resourcePath, Charset charset) throws IOException
resourcePath
- the path to the properties filecharset
- the character set to useNullPointerException
- resource path is nullIOException
- no such resource, or an I/O error occurred while
reading the filepublic static MessageSource fromFile(File file) throws IOException
file
- the file to read fromNullPointerException
- file is nullFileNotFoundException
- file does not exist, or cannot access fileIOException
- failed to read from filepublic static MessageSource fromPath(String path) throws IOException
This essentially calls fromFile(File)
.
path
- the file pathNullPointerException
- path is nullIOException
- see fromFile(File)
public static MessageSource fromFile(File file, Charset charset) throws IOException
file
- the file to read fromcharset
- the character set to useNullPointerException
- file is nullFileNotFoundException
- file does not exist, or cannot access fileIOException
- failed to read from filepublic static MessageSource fromPath(String path, Charset charset) throws IOException
This essentially calls fromFile(File, Charset)
.
path
- the file pathcharset
- the character setNullPointerException
- path is nullIOException
- see fromFile(File)
public String getKey(String key)
MessageSource
Note that this method MUST return null
if there is no match
for the given key.
Note also that it is guaranteed that you will never get a null key.
getKey
in interface MessageSource
key
- the key