021.8 properties (high frequency of development and use)

Contents: Properties basic access, storage to persistent devices, reading from persistent devices, simple simulation of paid software trial end
##
Properties - has the function of configuration files.
    Features:
    1. A subclass of Hash table, all methods in the map collection can be used
    . 2. This collection has no generic type. The keys and values ​​are all strings
    . 3. It is a persistent attribute set. The key value can be saved to the collection and the persistent device. The source of the key value can also be the persistent device.

################################################## ########
Basic Access

public class PropertiesDemo
{
    public static void main(String[] args)
    {
        methodDemo();
    }
    public static void methodDemo()
    {
        // Properties basic access 
        Properties prop = new Properties();
        prop.setProperty("xlsg", "21");
        prop.setProperty( "qldk", "20" );
 //         prop.list(System.out);    // This method is debugged using 
        Set<String> set = prop.stringPropertyNames();
         for (String name:set){
            String value = prop.getProperty(name);
            System.out.println(name+"__"+value);
        }
    }
}

 

################################################## ######################
Store to persistent device

Specify which file to save to through the store method of Properties

public class PropertiesDemo
{
    public static void main(String[] args) throws IOException
    {
        methodDemo();
    }
    
    public static void methodDemo() throws IOException
    {
        // Properties basic access 
        Properties prop = new Properties();
        prop.setProperty("xlsg", "21");
        prop.setProperty("qldk", "20");
        
//         prop.list(System.out);    // This method is debugged using 
        
        Set <String> set = prop.stringPropertyNames();
         for (String name:set){
            String value = prop.getProperty(name);
            System.out.println(name+"__"+value);
        }
        
        // Permanently store on the device 
        FileOutputStream fos = new FileOutputStream("myfile\\we.properties" );
        
        // Use the store method of properties. 
        prop.store(fos, "my demo , person info" );
    }

}

 

################################################## #######################################
Reading from persistent device

 

public static void main(String[] args) throws IOException
{
//        methodDemo();
    methodDemo_read();
}

private static void methodDemo_read() throws IOException
{
    File configure_file = new File("myfile\\we.properties" );
     // Read the data in the stream 
    Properties prop = new Properties();
    
    // Define read stream and data file association 
    FileInputStream fis = new FileInputStream(configure_file.toString());
    prop.load(fis);
    
    prop.list(System.out); //测试使用
    
    prop.setProperty("xlsg", "22");
    
    prop.list(System.out);
    
    FileOutputStream fos = new FileOutputStream(configure_file);
    
    prop.store(fos, "my demo,person info");
    
    fis.close();
    fos.close();
}

Steps: 1) Create a properties object
    2) Create an input stream object
    3) Associate the properties object with the input stream object
    4) Operate through prop.setProperty("xlsg", "22");
    5) Save prop.store(fos, "my demo , person info");

 ################################################## #######################################
Simple simulation fee software trial ended

public static void main(String[] args) throws IOException
{
    String value = read_count();
    if(count(Integer.parseInt(value))){
        //操作
        value = Integer.toString(Integer.parseInt(value)+1);
        System.out.println(value);
    }
    else{
        System.out.println( "The trial is over, please recharge!" );
    }
    write_count(value);
}

private static boolean count(int num)
{
    if(num<5){
        return true;
    }
    return false;
}

private static void write_count(String value) throws IOException
{
    File configure_file = new File("myfile\\count.properties");
    FileOutputStream fos = new FileOutputStream(configure_file);
    Properties prop = new Properties();
    prop.setProperty("count", value);
    prop.store(fos, "tryout count");
    fos.close();
}

private static String read_count() throws IOException
{
    File configure_file = new File("myfile\\count.properties");
    if(!configure_file.exists()){
        configure_file.createNewFile();
    }
    FileInputStream fis = new FileInputStream(configure_file);
    
    Properties prop = new Properties();
    prop.load(fis);
    String value;
    String key = "count";
    if(prop.containsKey(key)){
    }
    else{
        prop.setProperty(key, "0");
    }
    value = prop.getProperty(key);
    return value;

}
my code
public static void main(String[] args) throws IOException {
    /*
     * Exercise: Define the number of times the function recorder runs. After the trial times are met, a prompt will be given: The trial times have arrived, please register.
     *
     * Ideas:
     *1, counter is required. This software uses one count at a time. Every time it is used, it counts and accumulates.
     * 2, the counter is a variable in the program. The program starts the counter to count, but when the program ends, the counter disappears.
     * The count will be re-counted at the next startup, and the original count value will not be retained. what to do?
     * 3, make this counter persistent. Stored in a file, in order to identify the readability of the data, the data is given a name. A key-value pair appears.
     * And it is also a persistent key-value pair, and the Properties collection just meets this requirement.
     *
     */
    if(isStop()){
        System.out.println( "The trial times have arrived, please register" );
         return ;
    }
    runcode();
}

private  static  boolean isStop() throws IOException {
     // 1, configuration file. 
    File configFile = new File("tempfile\\app.properties" );
     if (!configFile.exists()){ // If the configuration file does not exist, create it. 
        configFile.createNewFile();
    }
    // 2, Create an attribute set. 
    Properties prop = new Properties();
     // 3, define the read stream and configuration file association. 
    FileInputStream fis = new FileInputStream(configFile);
     // 4, read the data associated with the stream into the attribute set. 
    prop.load(fis);
     // 5, Get the specific number of times through the specified key count of the attribute set. 
    String value = prop.getProperty("count" );
     int count = 0 ;
     // 6, Judge the value and increment it if it exists. 
    if (value!= null ){
        count = Integer.parseInt(value);
        if(count>=5){
            return true;
        }
    }
    count ++; // Increment its value.
    // 7, Restore the auto-incremented value and the specified key to the attribute set, the key is the same, and the value is overwritten. 
    prop.setProperty("count" , Integer.toString(count));
     // 8, store the property set in the configuration file. Update the information in the configuration file. 
    FileOutputStream fos = new FileOutputStream(configFile);
    prop.store(fos, "app run count" );
     // 9, close the resource. 
    fos.close();
    fis.close();
    return false;
}
// Program body. 
public  static  void runcode(){
    System.out.println( "Program running....play" );
}
the answer

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325884588&siteId=291194637