Properties與IO 相結合的方法
| 方法名 | 說明 |
|---|---|
| void load(InputStream inStream) | 從輸入位元組流讀取屬性串列(鍵和元素對) |
| void load(Reader reader) | 從輸入字符流讀取屬性串列(鍵和元素對) |
| void store(OutputStream out,String comments) | 將此屬性串列(鍵和元素對)寫入此Properties表中,以適合于使用load(InputStream )方法的格式寫入輸出位元組流 |
| void store(Writer writer,String comments) | 將此屬性串列(鍵和元素對)寫入此Properties表中,以適合于使用 load(Reader reader)方法的格式寫入輸出位元組流 |
字符流演示:
/*
* Properties與IO 相結合的方法(字符流用法)
?
| 方法名 | 說明 |
| -------------------------------------------- | ------------------------------------------------------------ |
| void load(InputStream inStream) | 從輸入位元組流讀取屬性串列(鍵和元素對) |
| void load(Reader reader) | 從輸入字符流讀取屬性串列(鍵和元素對) |
| void store(OutputStream out,String comments) | 將此屬性串列(鍵和元素對)寫入此Properties表中,以適合于使用load(InputStream )方法的格式寫入輸出位元組流 |
| void store(Writer writer,String comments) | 將此屬性串列(鍵和元素對)寫入此Properties表中,以適合于使用 load(Reader reader)方法的格式寫入輸出位元組流 |
* */
public class Demo01 {
public static void main(String[] args) throws IOException {
//把集合中的資料保存在檔案中
mystore();
//把檔案中的資料加載到集合
myLoad();
}
?
private static void myLoad() throws IOException {
//創建集合物件
Properties pt=new Properties();
FileReader fr=new FileReader("E:\\abc.txt");
pt.load(fr);
fr.close();
System.out.println(pt);
}
private static void mystore() throws IOException {
//創建集合物件
Properties pt=new Properties();
//添加元素
pt.setProperty("001","張三");
pt.setProperty("002","李四");
pt.setProperty("003","王五");
//創建檔案物件
FileWriter fw=new FileWriter("E:\\abc.txt");
//將集合中的資料加入到檔案
pt.store(fw,null);
//釋放資源
fw.close();
}
}
位元組流演示
/*
* Properties與IO 相結合的方法(位元組流用法)
?
| 方法名 | 說明 |
| -------------------------------------------- | ------------------------------------------------------------ |
| void load(InputStream inStream) | 從輸入位元組流讀取屬性串列(鍵和元素對) |
| void load(Reader reader) | 從輸入字符流讀取屬性串列(鍵和元素對) |
| void store(OutputStream out,String comments) | 將此屬性串列(鍵和元素對)寫入此Properties表中,以適合于使用load(InputStream )方法的格式寫入輸出位元組流 |
| void store(Writer writer,String comments) | 將此屬性串列(鍵和元素對)寫入此Properties表中,以適合于使用 load(Reader reader)方法的格式寫入輸出位元組流 |
* */
public class Demo02 {
public static void main(String[] args) throws IOException {
//把集合中的資料加載到檔案
myStore();
//把檔案中的資料加載到集合
myLoad();
}
private static void myLoad() throws IOException {
//創建集合物件
Properties pt=new Properties();
//創建位元組輸入流
InputStreamReader ipsr=new InputStreamReader(new FileInputStream("E:\\abcde.txt"));
pt.load(ipsr);
ipsr.close();
System.out.println(pt);
}
private static void myStore() throws IOException {
//創建集合物件
Properties pt=new Properties();
//添加資料
pt.setProperty("001","張三");
pt.setProperty("002","李四");
pt.setProperty("003","王五");
//創建位元組輸出流物件
OutputStreamWriter opsw=new OutputStreamWriter(new FileOutputStream("E:\\abcde.txt"));
pt.store(opsw,null);
opsw.close();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/458104.html
標籤:其他
上一篇:Java-GUI編程之處理位圖
