努力したWiki

推敲の足りないメモ書き多数

ユーザ用ツール

サイト用ツール


Action disabled: source
documents:java:java-001

propertiesファイルの読み込みサンプル

太陽生命さん、この記事はJava7の頃の物です。Java11やJava17の場合の調査を行う事を推奨します

2015/10/22
自分用。仕事でpropertiesファイルに日本語を含ませる必要が出て調べた。

参照参考

環境

仕事で使っているバージョンに合わせた。

H:\sample>java -version
java version "1.7.0_80"
Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
 
H:\sample>javac -version
javac 1.7.0_80
 
H:\sample>

valueが英字のみの場合

以下のpropertiesファイルを用意。

PropertiesSample.properties
# this is test
caption = this is test
comment = this is comment

読み出すコードはこちら。

ReadPropertiesSample.java
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class ReadPropertiesSample {
 
	private static Properties configuration = new Properties();
	private static String path = "PropertiesSample.properties";
 
	static {
		try{
			InputStream cf = ReadPropertiesSample.class.getResourceAsStream(path);
			configuration.load(cf);
			cf.close();
		}
		catch(IOException e){
			System.out.println(e.getStackTrace());
		}
	}
 
	public static void main(String[] args) {
		System.out.printf("%s=[%s]\n", "caption",configuration.getProperty("caption"));
		System.out.printf("%s=[%s]\n", "comment",configuration.getProperty("comment"));
	}
 
}

実行結果はこうなる。

H:\sample>javac ReadPropertiesSample.java
H:\sample>java ReadPropertiesSample
caption=[this is test]
comment=[this is comment]
 
H:\sample>

valueに日本語(漢字)が含まれる場合その1

調べるとJavaSE6以降では日本語OKということだけど、古いコードのままだと読めない。
以下のpropertiesファイルを用意。エンコーディングはUTF-8。

PropertiesSample.properties
# this is test
caption = これはテストです
comment = これはコメントです

同じコードを実行してみると、文字化けが起こる。

H:\sample>java ReadPropertiesSample
caption=[???????? ̄???????????§???]
comment=[???????? ̄??????????????§???]
 
H:\sample>

propertiesファイルは元々ISO 8859-1エンコーディングで作成されていた。古いコードのままだとその前提で処理を行ってしまい文字化けする模様。
コードを修正できない場合は、propertiesファイルにUnicodeエスケープで日本語(漢字)部分を記述する事で一応対処できる。

PropertiesSample.properties
# this is test
caption = \u3053\u308C\u306F\u30C6\u30B9\u30C8\u3067\u3059
comment = \u3053\u308C\u306F\u30B3\u30E1\u30F3\u30C8\u3067\u3059
H:\sample>java ReadPropertiesSample
caption=[これはテストです]
comment=[これはコメントです]
 
H:\sample>

valueに日本語(漢字)が含まれる場合その2

コードを修正できるなら、日本語対応してしまう。
具体的には、Properties#loadメソッドに、UTF-8エンコーディングを指定したInputStreamReaderオブジェクトを渡してやる。

ReadPropertiesSample.java
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
 
public class ReadPropertiesSample {
 
	private static Properties configuration = new Properties();
	private static String path = "PropertiesSample.properties";
 
	static {
		try{
			InputStreamReader cf = new InputStreamReader( ReadPropertiesSample.class.getResourceAsStream(path), "UTF-8" ) ;
			configuration.load(cf);
			cf.close();
		}
		catch(IOException e){
			System.out.println(e.getStackTrace());
		}
	}
 
	public static void main(String[] args) {
		System.out.printf("%s=[%s]\n", "caption",configuration.getProperty("caption"));
		System.out.printf("%s=[%s]\n", "comment",configuration.getProperty("comment"));
	}
 
}

propertiesファイルはUnicodeエスケープと直接記述の二つのエントリを書いてみる。

PropertiesSample.properties
# this is test
caption = これはテストです
comment = \u3053\u308C\u306F\u30B3\u30E1\u30F3\u30C8\u3067\u3059

実行すると、直接日本語を記述してもUnicodeエスケープで日本語(漢字)を記述しても、対応できていることがわかった。

H:\sample>javac ReadPropertiesSample.java
H:\sample>java ReadPropertiesSample
caption=[これはテストです]
comment=[これはコメントです]
 
H:\sample>
documents/java/java-001.txt · 最終更新: 2023/08/14 07:12 by k896951

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki