文字コードの違いによる、ファイルの読み込みデータの違いにつきまして、やりたいこととしましてはプロパティ設定ファイル(テキストファイル)を読み込み、対応する値を出力させたいのですが、以下のコードがその処理となります。
Java
1package test; 2 3public class Main { 4 5 public static void main(String [] args) { 6 //プロパティファイル読み込み 7 Properties properties =Properties.getInstance("settings/p.txt"); 8 9 //値を取得し、出力 10 System.out.println(properties.getValue("aaa")); 11 System.out.println(properties.getValue("bbb")); 12 System.out.println(properties.getValue("ccc")); 13 } 14} 15
Java
1package test; 2 3import java.io.BufferedReader; 4import java.io.File; 5import java.io.FileReader; 6import java.io.IOException; 7import java.util.ArrayList; 8import java.util.HashMap; 9import java.util.List; 10import java.util.Map; 11 12/** 13 * プロパティファイル読み込み処理 14 */ 15public class Properties { 16 17 /** ファイルごとのプロパティインスタンス */ 18 private static Map<String, Properties> propertiesInstance; 19 20 /** プロパティファイルの中身 */ 21 private Map<String , String> property; 22 23 /** keyとvalueを分ける文字 */ 24 public static final String KEY_VALUE_SEPARATOR = "="; 25 26 /** 27 * コンストラクタ 28 * @param filePath 29 */ 30 public Properties(String filePath) { 31 32 try { 33 34 //filePathの内容を読み込む 35 String [] fileContent = fileRead(filePath); 36 37 //プロパティHashmap初期化 38 this.property = new HashMap<String, String>(); 39 40 for(String line : fileContent) { 41 42 if(line.indexOf(KEY_VALUE_SEPARATOR) == -1) { 43 //行中に = を含まない場合は次の行に進む 44 continue; 45 } 46 // = で分割し、左側をkey 右側をvalueとして入れる 47 String [] keyValue = line.split(KEY_VALUE_SEPARATOR); 48 this.property.put(keyValue[0], keyValue[1]); 49 } 50 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 } 55 56 /** 57 * インスタンス取得(シングルトン) 58 * @param filePath 59 * @return 60 */ 61 public static Properties getInstance(String filePath) { 62 if(propertiesInstance == null) { 63 //プロパティインスタンス保持Hashmap初期化 64 propertiesInstance = new HashMap<String, Properties>(); 65 } 66 if(propertiesInstance.containsKey(filePath) == true) { 67 //すでにファイルを読み込んだ場合 68 return propertiesInstance.get(filePath); 69 } else { 70 //新しくファイルを読み込む場合 71 Properties newInstance = new Properties(filePath); 72 propertiesInstance.put(filePath, newInstance); 73 return newInstance; 74 } 75 } 76 77 /** 78 * プロパティフィルのkey値に対するvalue値取得 79 * @param key 80 * @return 81 */ 82 public String getValue(String key) { 83 return this.property.get(key); 84 } 85 86 /** 87 * ファイル読み込み処理 88 * @param filePath 89 * @return 90 * @throws IOException 91 */ 92 public String [] fileRead(String filePath) throws IOException { 93 94 //ファイルの中身を1行ずつ読み込み、Stringの配列に変換する 95 File file = new File(filePath); 96 FileReader fileReader = new FileReader(file); 97 BufferedReader bufferedReader = new BufferedReader(fileReader); 98 99 List<String> readData = new ArrayList<String>(); 100 101 String line; 102 103 while((line = bufferedReader.readLine()) != null) { 104 readData.add(line); 105 } 106 107 fileReader.close(); 108 109 return readData.toArray(new String[] {}); 110 } 111} 112
プロパティ設定ファイルのデータは下記の通りです。
test
1aaa=111 2bbb=222 3ccc=Settings
プロパティ設定ファイルのデータをShift-JISで保存して実行した場合の実行結果は下記の通りとなり、期待通りの結果となります。
111
222
Settings
しかし、読み込みデータをUTF-8で保存した場合の実行結果は、
null
222
Settings
となり、一番最初の行のデータのみ値の取得ができません。(読み込みデータの先頭に1行空白を入れればすべて読み込めるのですが…)
Linux上で動かしたいので、設定ファイルの文字コードはUTF-8としたいのですが、どのようにすれば1行目から正常に読み込めるようになるのでしょうか。
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー