public final class PropertiesMessageSource extends java.lang.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(java.io.File file)
Create a message source from a properties file on the filesystem using
UTF-8
|
static MessageSource |
fromFile(java.io.File file,
java.nio.charset.Charset charset)
Create a message source from a properties file on the filesystem using
the specified charset
|
static MessageSource |
fromPath(java.lang.String path)
Create a message source from a properties file on the filesystem using
the specified encoding
|
static MessageSource |
fromPath(java.lang.String path,
java.nio.charset.Charset charset)
Create a message source from a properties file on the filesystem using
the specified charset
|
static MessageSource |
fromResource(java.lang.String resourcePath)
Create a message source from a classpath resource using UTF-8
|
static MessageSource |
fromResource(java.lang.String resourcePath,
java.nio.charset.Charset charset)
Create a message source from a classpath resource using the specified
charset
|
java.lang.String |
getKey(java.lang.String key)
Return a message matching a given key
|
public static MessageSource fromResource(java.lang.String resourcePath) throws java.io.IOException
resourcePath
- the path to the properties filejava.lang.NullPointerException
- resource path is nulljava.io.IOException
- no such resource, or an I/O error occurred while
reading the filepublic static MessageSource fromResource(java.lang.String resourcePath, java.nio.charset.Charset charset) throws java.io.IOException
resourcePath
- the path to the properties filecharset
- the character set to usejava.lang.NullPointerException
- resource path is nulljava.io.IOException
- no such resource, or an I/O error occurred while
reading the filepublic static MessageSource fromFile(java.io.File file) throws java.io.IOException
file
- the file to read fromjava.lang.NullPointerException
- file is nulljava.io.FileNotFoundException
- file does not exist, or cannot access filejava.io.IOException
- failed to read from filepublic static MessageSource fromPath(java.lang.String path) throws java.io.IOException
This essentially calls fromFile(File)
.
path
- the file pathjava.lang.NullPointerException
- path is nulljava.io.IOException
- see fromFile(File)
public static MessageSource fromFile(java.io.File file, java.nio.charset.Charset charset) throws java.io.IOException
file
- the file to read fromcharset
- the character set to usejava.lang.NullPointerException
- file is nulljava.io.FileNotFoundException
- file does not exist, or cannot access filejava.io.IOException
- failed to read from filepublic static MessageSource fromPath(java.lang.String path, java.nio.charset.Charset charset) throws java.io.IOException
This essentially calls fromFile(File, Charset)
.
path
- the file pathcharset
- the character setjava.lang.NullPointerException
- path is nulljava.io.IOException
- see fromFile(File)
public java.lang.String getKey(java.lang.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