Created
September 14, 2014 18:05
-
-
Save komiya-atsushi/0294e4a2fcffabe1b3e2 to your computer and use it in GitHub Desktop.
UTF-8 エンコーディングされたプロパティファイルを Properties クラスで取り扱う。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.Properties; | |
/** | |
* UTF-8 エンコーディングされたプロパティファイルを {@link Properties} クラスで取り扱う。 | |
*/ | |
public class PropertiesWithUtf8 { | |
static Properties loadUtf8Properties(String resourceName) throws IOException { | |
try (InputStream is = PropertiesWithUtf8.class.getResourceAsStream(resourceName); | |
InputStreamReader isr = new InputStreamReader(is, "UTF-8"); | |
BufferedReader reader = new BufferedReader(isr)) { | |
Properties result = new Properties(); | |
// Properties#load() で渡す Reader オブジェクトを UTF-8 エンコーディング指定して生成した | |
// InputStreamReader オブジェクトにする | |
result.load(reader); | |
return result; | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
Properties prop = loadUtf8Properties("/utf8.properties"); | |
System.out.println(prop.getProperty("いろはにほへと")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment