IOストリームと集合の交差Propertiesクラスの紹介.
4509 ワード
PropertiesクラスはHashtableを継承し,HashtableはMapインタフェースを実現した.このクラスのキー値ペアは文字列です.
Propertiesの構築方法:Properties pro=new Properties();//Propertiesコレクションを作成します.
一般的な方法:String setProperties(String key,String value);//要素を追加し、要素を変更します.
String setProperties(String oldKey,String newValue);//修正するキーを使って、新しい文字列値を付ければいいです.キーの古い値を返し、キーが存在しない場合nullを返します.
String getPropery(String key);//指定したキーの値を返します.
Set stringPropertyNames();//セットのキーセットを返します
void list(PrintStream out);//プロパティ・リストを指定した出力ストリームに出力します.この方法では、テストが頻繁に使用されます.
void store(OutputStream out,String comments);//この
次のようになります.
void load(InputStream inStream);//入力ストリームからキー値ペアを読み込みます.
デモ:
Propertiesの構築方法:Properties pro=new Properties();//Propertiesコレクションを作成します.
一般的な方法:String setProperties(String key,String value);//要素を追加し、要素を変更します.
String setProperties(String oldKey,String newValue);//修正するキーを使って、新しい文字列値を付ければいいです.キーの古い値を返し、キーが存在しない場合nullを返します.
String getPropery(String key);//指定したキーの値を返します.
Set
void list(PrintStream out);//プロパティ・リストを指定した出力ストリームに出力します.この方法では、テストが頻繁に使用されます.
void store(OutputStream out,String comments);//この
Properties
テーブルのプロパティリスト(キーと要素のペア)を出力ストリームに書き込みます.次のようになります.
public static void show_3() throws IOException {
Properties pro = new Properties();
pro.setProperty("zhangsan", "20");
pro.setProperty("lisi", "29");
pro.setProperty("zhailou", "24");
pro.setProperty("wangwu", "25");
FileOutputStream fos = new FileOutputStream("info.txt");
// 。
pro.store(fos, "name_age");
}
void load(InputStream inStream);//入力ストリームからキー値ペアを読み込みます.
デモ:
package Properties;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
/**
* Properties :
* 1, 。
* 2, , 。
* 。
*/
// show_1();
// show_2();
// show_3();
// show_4();
// myLoad();
xiugai();
}
public static void xiugai() throws IOException {
Properties pro = new Properties();
File file = new File("info.txt");
if(!file.exists())
file.createNewFile();
FileReader fr =new FileReader(file);
//
pro.load(fr);
//
pro.setProperty("baiyang", "16");
// 。
FileWriter fw = new FileWriter(file);
pro.store(fw, "xiugaihou");
pro.list(System.out);
}
public static void myLoad() throws IOException {
Properties pro = new Properties();
BufferedReader bf = new BufferedReader(new FileReader("info.txt"));
String line = null;
while((line = bf.readLine())!=null){
if(line.startsWith("#"))
continue;
String []pr = line.split("=");
pro.setProperty(pr[0], pr[1]);
}
pro.list(System.out);
}
public static void show_4() throws IOException {
Properties pro = new Properties();
FileInputStream fis = new FileInputStream("info.txt");
// 。
pro.load(fis);
pro.list(System.out);
}
public static void show_3() throws IOException {
Properties pro = new Properties();
pro.setProperty("zhangsan", "20");
pro.setProperty("lisi", "29");
pro.setProperty("zhailou", "24");
pro.setProperty("wangwu", "25");
FileOutputStream fos = new FileOutputStream("info.txt");
// 。
pro.store(fos, "name_age");
}
public static void show_2() {
//
Properties pro = new Properties();
// 。
pro.setProperty("zhangsan", "20");
pro.setProperty("lisi", "29");
pro.setProperty("zhailou", "24");
pro.setProperty("wangwu", "25");
pro.list(System.out);
/**
* list : 。
* -- listing properties --
zhailou=24
zhangsan=20
lisi=29
wangwu=25
*/
}
public static void show_1() {
//
Properties pro = new Properties();
// 。
pro.setProperty("zhangsan", "20");
pro.setProperty("lisi", "29");
pro.setProperty("zhailou", "24");
pro.setProperty("wangwu", "25");
//
pro.setProperty("zahngsan", "10");
//
Set<String> names = pro.stringPropertyNames();
for(String name:names){
String value = pro.getProperty(name);
System.out.println(name+" : "+value);
}
}
}