java IO流(properties)

來源:互聯網
上載者:User

標籤:

/*
 * 1.Properties是hashtable的子類:
 * 他具備map集合的特點,而且它裡面儲存的鍵值對都是字串。
 * 是集合中和IO技術相結合的集合容器。
 * 該對象的特點:可以用於鍵值對形式的設定檔,操作硬碟上的鍵值對。
 * 
 * 2.將流中的資料存放區到集合中(load方法原理):
 * 用一個流和info.txt檔案關聯。
 * 讀取一行資料,將改行資料用“=”進行切割。
 * 等號左邊作為鍵,右邊作為值,存入到Properties集合中。
 * 載入資料時,資料需要固定格式,鍵=值。
 */
import java.util.*;
import java.io.*;
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
//GetAndSet();
//method_1();
loadeDemo_1();
}
//將流中的資料存放區到集合中
public static void loadeDemo_1() throws IOException
{
FileReader f=new FileReader("c:\\demo1.txt");
Properties pro=new Properties();
pro.load(f);
//列出集合目錄。
pro.list(System.out);
//sop(pro);
pro.setProperty("wangwu","39");
FileWriter fw=new FileWriter("c:\\demo1.txt");
pro.store(fw,"hahahaha");
pro.list(System.out);
f.close();
fw.close();
}
public static void  method_1() throws IOException
{
Properties pro=new Properties();
BufferedReader buf=new BufferedReader(new FileReader("c:\\demo1.txt"));
String line=null;
while((line=buf.readLine())!=null)
{
String arr[]=line.split("=");

pro.setProperty(arr[0],arr[1]);
}
sop(pro);
}
public static void GetAndSet()
{
Properties pro=new Properties();
pro.setProperty("zhangsan","30");
pro.setProperty("lisi","25");
//sop(pro);
//sop(pro.getProperty("lisi"));
pro.setProperty("lisi",99+"");
for(String s:pro.stringPropertyNames())
{
sop(s+"---"+pro.getProperty(s));
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}

}


/*
 * 需求:
 * 用於記錄應用程式運行次數,如果使用次數已到,給出註冊提示。
 * 需要一個設定檔,儲存每次使用的次數,檔案中資料使用鍵值對的形式。
 * 設定檔可以實現應用程式資料的共用。
 */
import java.io.*;
import java.util.*;
public class PropertiesDemo2 {
public static void main(String[] args) throws IOException {
File f=new File("c://demo2.ini");
if(!f.exists())
f.createNewFile();
FileInputStream fi=new FileInputStream(f);
Properties pro=new Properties();
pro.load(fi);
String value=pro.getProperty("time");
int count=0;
if(value!=null)
{

count=Integer.parseInt(value);
if(count>=5)
sop("試用次數已滿,請購買正式版本");
}
count++;
pro.setProperty("time",count+"");
FileOutputStream fos=new FileOutputStream(f);
pro.store(fos,"lol");
fi.close();
fos.close();

public static void sop(Object obj)
{
System.out.println(obj);
}
}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

java IO流(properties)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.