Properties
Properties作用
・設定ファイル
Java
#sql
jdbc.sql.driver=net.sourceforge.jtds.jdbc.Driver
jdbc.sql.url=jdbc:jtds:sqlserver://localhost:1433/test
jdbc.sql.username=sa
jdbc.sql.password=1234
解析方法1
import java.util.Locale;
import java.util.ResourceBundle;
public class ResourceUtils {
private static ResourceBundle rb = null;
private static final String PROPERTY_FILE = "config";
public static String driver = null;
public static String url = null;
public static String username = null;
public static String password = null;
static {
rb = ResourceBundle.getBundle(PROPERTY_FILE, Locale.getDefault());
driver = rb.getString("jdbc.sql.driver");
url = rb.getString("jdbc.sql.url");
username = rb.getString("jdbc.sql.username");
password = rb.getString("jdbc.sql.password");
}
public static String loadProperty(String key) {
rb = ResourceBundle.getBundle(PROPERTY_FILE, Locale.getDefault());
return rb.getString(key);
}
}
解析方法2
import java.util.Properties;
Properties prop = new Properties();
try {
prop.load(new FileInputStream(new File("C:\temp\config.properties")));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String driver = prop.getProperty("jdbc.sql.driver");
...
★properties/xmlファイルを書き込む
public void writeXmlFile() throws FileNotFoundException, IOException {
Properties prop = new Properties();
prop.setProperty("AAA", "123");
prop.setProperty("BBB", "456");
//OutputStream output = new FileOutputStream("D:\\write.properties")
//PrintWriter output = new PrintWriter("D:\\write.properties")
try (OutputStream output
= new FileOutputStream(
new File("D:\\write.properties或いはwrite.xml"))) {
prop.store(output, "comment");
prop.storeToXML(output, "comment");
}
}
XMLファイルのフォーマット
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>comment</comment>
<entry key="AAA">123</entry>
<entry key="BBB">456</entry>
</properties>
★properties/xmlファイルを読み込む
public void readFile() throws IOException {
Properties prop = new Properties();
try (InputStream input
= new FileInputStream(
new File("D:\\read.properties或いはread.xml"))) {
prop.load(input);
prop.loadFromXML(input);
prop.list(System.out);
// 書き方1
Enumeration<String> en = (Enumeration<String>) prop.propertyNames();
while (en.hasMoreElements()) {
String key = en.nextElement();
String value = prop.getProperty(key);
...
}
// 書き方2
Enumeration<Object> en = prop.keys();
while (en.hasMoreElements()) {
String key = (String)en.nextElement();
String value = prop.getProperty(key);
...
}
}
}
Java
★システムプロパティについて
標準改行コードを取得
System.getProperty("line.separator")
ファイルの区切り文字
File.pathSeparator パス区切り文字
Windows ;
Unix/Linux :
File.separator ファイル区切り文字
Windows ¥
Unix/Linux /
file.separator ファイル区切り文字 ("/" on UNIX, "\" on Windows)
line.separator 改行区切り文字 ("\n" on UNIX and Windows)
path.separator パス区切り文字 (":" on UNIX, ";" on Windows)
java.class.path Java クラスパス
java.class.version Java クラスバージョン
java.home Java インストールディレクトリ
java.vendor Java ベンダー情報
java.vendor.url Java ベンダー URL
java.version Java バージョン
os.arch OSアーキテクチャ
os.name OS名
os.version
user.dir ユーザの現在の作業ディレクトリ
user.home ユーザのホームディレクトリ
user.name ユーザアカウント名
java.vm.specification.version
java.vm.specification.vendor
java.vm.specification.name
java.vm.version
java.vm.vendor
java.vm.name
java.specification.version
java.specification.vendor
java.specification.name
java.ext.dirs Path of extension
CSVファイルのエスケープ処理
Apache Commonsを利用するサンプル
import org.apache.commons.lang.StringEscapeUtils;
static String escape(String str){
if(str == null){
return "";
}
str = StringEscapeUtils.escapeCsv(str);
// CRLFとCR ⇒ LF(旧Macも対応)
str = str.replaceAll("\r\n|\r", "\n");
return str;
}
システムプロパティ一覧を出力
〇順序なしで出力
Properties props = System.getProperties();
props.list(System.out);
〇アルファベット順で出力
Properties p = System.getProperties();
TreeMap map = new TreeMap();
map.putAll(p); // Propertiesがキャストできないため、Genericsは×
// map.keySet()の戻り値はSet<Object>のため、foreach文は×
Iterator itr = map.keySet().iterator();
while (itr.hasNext()) {
String key = (String)itr.next();
String value = (String)map.get(key);
System.out.println(key + "=" + value);
}