VirtualBox を暫く前から使っている。kvm との区別は? という気もするが、デスクトップから仮想マシンを操作したい場合はVirtualBox という感じ。サーバに載せる仮想マシンは専ら kvm である。
以前は Linux から仮想マシン経由で Windows を動かそうとしたら VMWare 一択しかなくてそれなりにお布施をした記憶がある。よい時代になったものである。
あ、そういえば フルスクリーンから抜けるのはデフォルトで 右Ctrl + F なので忘れないように > 自分
2011/12/04
2011/12/01
ini file parser for Java
http://ini4j.sourceforge.net/
Java で ini ファイル が使われることはあまりないようだ。
勿論 標準で java.util.Properties というクラスがあり、Key と Value で設定を扱えるだけで十分という話はある。けれども、設定をグループ化するには、設定項目に名前空間を付与する等工夫が必要だ。もっと複雑な構造にしたければXMLで、、という話になるかもしれない。
ini ファイルは「セクション」があることで設定をグループ化することができる。(Zend_Config_Ini の如く設定の継承とかあればかなり便利だと思うが)これだけの違いのためにiniファイルのパーサを書くのはちょっと、、と思ったときに ini4j を見つけた。
基本的に設定ファイルをロードした後は、IniPreferences 経由で自由にセクションと Key, Value を扱える。
all_ini_entries = new Ini(new File(fileName));
ini_prefs = new IniPreferences(all_ini_entries);
最低限の機能を class として書き下すと以下のようになる。単純なパースや書き込み以外にも様々な機能が揃っているので、チュートリアルも是非見ると良いと思います。
Java で ini ファイル が使われることはあまりないようだ。
勿論 標準で java.util.Properties というクラスがあり、Key と Value で設定を扱えるだけで十分という話はある。けれども、設定をグループ化するには、設定項目に名前空間を付与する等工夫が必要だ。もっと複雑な構造にしたければXMLで、、という話になるかもしれない。
ini ファイルは「セクション」があることで設定をグループ化することができる。(Zend_Config_Ini の如く設定の継承とかあればかなり便利だと思うが)これだけの違いのためにiniファイルのパーサを書くのはちょっと、、と思ったときに ini4j を見つけた。
基本的に設定ファイルをロードした後は、IniPreferences 経由で自由にセクションと Key, Value を扱える。
all_ini_entries = new Ini(new File(fileName));
ini_prefs = new IniPreferences(all_ini_entries);
最低限の機能を class として書き下すと以下のようになる。単純なパースや書き込み以外にも様々な機能が揃っているので、チュートリアルも是非見ると良いと思います。
This file contains 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
package com.example.ini; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.Set; | |
import java.util.prefs.BackingStoreException; | |
import java.util.prefs.Preferences; | |
import org.ini4j.Ini; | |
import org.ini4j.IniPreferences; | |
import org.ini4j.InvalidFileFormatException; | |
public class ExampleIniConfig { | |
/* | |
* Ini オブジェクト | |
*/ | |
private static Ini all_ini_entries = null; | |
/* | |
* 設定オブジェクト | |
*/ | |
private static Preferences ini_prefs = null; | |
/* | |
* 設定ファイルパス(クラスパスが通った場所からの相対パス) | |
*/ | |
private static String CONFIG_FILE = "com/example/ini/example.ini"; | |
/* | |
* 設定ファイルを読み込みます。 | |
* | |
* @param filename ファイル名(クラスパスが通った場所からの相対パス) | |
*/ | |
private static void load(String fileName) throws IOException, InvalidFileFormatException { | |
ClassLoader cloader = Thread.currentThread().getContextClassLoader(); | |
if (cloader == null) { | |
cloader = ExampleIniConfig.class.getClassLoader(); | |
} | |
all_ini_entries = new Ini(new File(fileName)); | |
ini_prefs = new IniPreferences(all_ini_entries); | |
} | |
/** | |
* 特定のセクションの設定全てを Preferences オブジェクトとして返します | |
* | |
* @param sectionName - セクション名 | |
* @return 設定オブジェクト | |
*/ | |
public static Preferences getSection(String sectionName) { | |
if (ini_prefs == null) { | |
try { | |
load(CONFIG_FILE); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
return ini_prefs.node(sectionName); | |
} | |
/** | |
* セクション名全てを配列として返します | |
* | |
* @return セクション名の配列 | |
*/ | |
public static String[] getAllSections() { | |
if (ini_prefs == null) { | |
try { | |
load(CONFIG_FILE); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
Set<String> all_section_names = all_ini_entries.keySet(); | |
return (String[])all_section_names.toArray(new String[0]); | |
} | |
/** | |
* 特定のセクションが存在するか否かを判定します | |
* | |
* @param sectionName - セクション名 | |
* @return セクションが存在すれば true 存在しなければ false | |
*/ | |
public static boolean sectionExists(String sectionName) { | |
if (ini_prefs == null) { | |
try { | |
load(CONFIG_FILE); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
try { | |
return ini_prefs.nodeExists(sectionName); | |
} catch (BackingStoreException e) { | |
e.printStackTrace(); | |
return false; | |
} | |
} | |
/** | |
* 特定の設定をboolean値として取得します | |
* | |
* @param sectionName - セクション名 | |
* @param key - 設定のキー | |
* @return 設定値。設定が存在しない場合はfalse | |
*/ | |
public static boolean getBoolean(String sectionName, String key) { | |
Preferences section = getSection(sectionName); | |
return section.getBoolean(key, false); | |
} | |
/** | |
* 特定の設定をString値として取得します | |
* | |
* @param sectionName - セクション名 | |
* @param key - 設定のキー | |
* @return 設定値。設定が存在しない場合は空文字列 | |
*/ | |
public static String get(String sectionName, String key) { | |
Preferences section = getSection(sectionName); | |
return section.get(key, ""); | |
} | |
} |
登録:
投稿 (Atom)