Skip to content

Instantly share code, notes, and snippets.

@komiya-atsushi
Created September 14, 2014 18:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komiya-atsushi/0294e4a2fcffabe1b3e2 to your computer and use it in GitHub Desktop.
Save komiya-atsushi/0294e4a2fcffabe1b3e2 to your computer and use it in GitHub Desktop.
UTF-8 エンコーディングされたプロパティファイルを Properties クラスで取り扱う。
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