天天看點

javaday50_IO流_Properties學習與應用場景

一.Properties基礎方法學習

package java_Studing_day50_IO流10;

import java.io.BufferedInputStream;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStream;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Properties;

import java.util.Set;

public class propertiesStudy {

   以下為學習代碼:

    public propertiesStudy() {

// TODO Auto-generated constructor stub

    }

    public static void main(String[] args) throws IOException {

//propertiesDemo();

//propertiesDemo2();

//propertiesDemo3();

//propertiesDemo4();

//myLoad();

         modify();

    }

// TODO Auto-generated method stub

   //1.修改已有的配置表中的資訊

    //讀取這個檔案

private static void modify() throws IOException { 

    File file=new File("F:\\PersistentConfigurationFile.txt");

    if(file.exists());

    BufferedReader bfr=new BufferedReader (new FileReader("F:\\PersistentConfigurationFile.txt"));

    Properties prop=new Properties();//建立配置表

   prop.load(bfr);//加載本地檔案到配置表

   prop.setProperty("Renekton", "6300");//修改配置表資訊

   BufferedWriter bfw=new BufferedWriter(new FileWriter("F:\\PersistentConfigurationFile.txt"));

   prop.store(bfw, "name&sale2ndModify"); //存儲到本地檔案中,完成更新

   bfr.close();

   bfw.close();

}  

public static void myLoad() throws IOException{

    Properties prop=new Properties();

    BufferedReader bfr=new BufferedReader(new FileReader("F:\\PersistentConfigurationFile.txt"));//關聯配置檔案

    String line=null;

    while((line=bfr.readLine())!=null){ 

if(line.startsWith("#")) //如果不是#開頭,則繼續,否則退出本次循環

   continue;

String []arr=line.split("=");//将第一行的資料用=分割,分别儲存在String[]的0位和1位

System.out.println(arr[0]+"-------------"+arr[1]); //列印這個數組

    }

    bfr.close();

}    

public  static void propertiesDemo4() throws IOException {

// 集合中的資料來自一個檔案中

        //注意:必須保證該檔案中的資料是鍵值對

        //需要使用到讀取流

    Properties prop=new Properties();

    FileInputStream fis=new FileInputStream("F:\\PersistentConfigurationFile.txt");

    BufferedInputStream bfr=new BufferedInputStream(fis);

    prop.load(bfr);//load和流關聯

    prop.list(System.out);

    }

public static void propertiesDemo3() throws IOException {

  //建立一個properties集合

    Properties prop=new Properties();

    //存儲元素

    prop.setProperty("VN", "4800");

    prop.setProperty("Jinx", "6300");

    prop.setProperty("Renekton", "4800");

    prop.setProperty("Ashe", "450");

    prop.setProperty("Poppy", "3150");

   //想要将這些集合中的字元串鍵值資訊持久化存儲到檔案中

    //需要關聯輸出流

   BufferedWriter bfw=new BufferedWriter(new FileWriter("F:\\PersistentConfigurationFile.txt"));

   prop.store(bfw, "HeroName+Sale");//Store和流關聯

   bfw.close();

}

 public static void propertiesDemo2(){

   //建立一個properties集合

     Properties prop=new Properties();

     //存儲元素

     prop.setProperty("VN", "4800");

     prop.setProperty("Jinx", "6300");

     prop.setProperty("Renekton", "4800");

     prop.setProperty("Ashe", "450");

     prop.setProperty("Poppy", "3150");

     prop=System.getProperties();

     //list方法,将配置清單中的資訊輸出到一個輸出流,這個在調試時很有用

     prop.list(System.out);//列印到控制台

 }   

public static void propertiesDemo(){

    //建立一個properties集合

    Properties prop=new Properties();

    //存儲元素

    prop.setProperty("VN", "4800");

    prop.setProperty("Jinx", "6300");

    prop.setProperty("Renekton", "4800");

    prop.setProperty("Ashe", "450");

    prop.setProperty("Poppy", "3150");

   //删除元素

    prop.remove("renekton");

    //修改元素

    prop.setProperty("VN", "6300");

    //取出所有元素

    Set<String> names=prop.stringPropertyNames();//傳回一個set集合

    for(String name:names){

String value=prop.getProperty(name);

System.out.println(name+"---------"+value);

    }

}

}

2.Properties集合的練習與總結

package java_Studing_day50_IO流10;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Properties;

public class PropertiesPractice {

    public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub

         getAppCount();//此方法在程式運作前運作,可以監控程式運作的次數

    }

    public static void getAppCount() throws IOException {

//将配置檔案封裝成File對象

File config=new File("F:\\Appcounter.properties");

if(config.exists()){

config.createNewFile();

}

FileInputStream fis=new FileInputStream(config);

Properties prop=new Properties();

//加載本地配置表 

prop.load(fis); 

 //從集合中通過鍵擷取次數,得到String類型資料

  String value=prop.getProperty("time");

  //建立計數器 

  int count=0;

  //如果

  if(value!=null){

count=Integer.parseInt(value);//将String類型的次數資訊轉Int型,然後傳給計數器

if(count>=5){

   System.out.println("免費試用次數已到,請注冊!謝謝!"); 

   throw new RuntimeException("使用次數已到,給錢!") ;

}

  }

   count++;

   //将改變後的次數從新存儲到集合中

   prop.setProperty("time", count+"");

   FileOutputStream fos=new FileOutputStream(config);

   //更新,存儲到本地

   prop.store(fos, "counterModify");

   fis.close();

   fos.close();

   }  

}

繼續閱讀