集册 Java实例教程 演示java.util包的类属性。

演示java.util包的类属性。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

562
演示java.util包的类属性。
// 来自 时代Java - nowjava.com

import java.io.FileOutputStream;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Properties;

import java.util.Set;


public class Main  

{

   public static void main(String[] args)

   {

      Properties table = new Properties(); 


      // set properties

      table.setProperty("color", "blue");
      /*
       from nowjava 
      */

      table.setProperty("width", "200");


      System.out.println("After setting properties");

      listProperties(table); 


      // replace property value

      table.setProperty("color", "red");


      System.out.println("After replacing properties");

      listProperties(table); 


      saveProperties(table);


      table.clear(); // empty table


      System.out.println("After clearing properties");

      listProperties(table); 

      

      loadProperties(table);


      // get value of property color

      Object value = table.getProperty("color");


      // check if value is in table

      if (value != null)

         System.out.printf("Property color's value is %s%n", value);

      else

         System.out.println("Property color is not in table");

   } 


   // save properties to a file

   private static void saveProperties(Properties props)

   {

      // save contents of table

      try

      {

         FileOutputStream output = new FileOutputStream("props.dat");

         props.store(output, "Sample Properties"); // save properties

         output.close();

         System.out.println("After saving properties");

         listProperties(props); 

      } 

      catch (IOException ioException)

      {

         ioException.printStackTrace();

      } 

   } 


   // load properties from a file

   private static void loadProperties(Properties props)

   {

      // load contents of table

      try

      {

         FileInputStream input = new FileInputStream("props.dat");

         props.load(input); // load properties

         input.close();

         System.out.println("After loading properties");

         listProperties(props); 

      } 

      catch (IOException ioException)

      {

         ioExc
展开阅读全文