java. util. Properties class

/**

  • Created by cxh on 17/07/21.
    */
    public class Main {
    public static void main(String[] args) throws Exception {
    Properties prop=new Properties();
    FileInputStream fis=new FileInputStream("/Users/cxh/IdeaProjects/JavaBaseTest/src/bin/sample.properties");
    prop.load(fis);
    prop.list(System.out);
    System.out.println();
    System.out.println("The property is : "+prop.getProperty("id"));
    }
    }

Test results:

The property is : cxh

Process finished with exit code 0

/**

  • Created by cxh on 17/07/21.
    */
    public class Main {
    public static void main(String[] args) {
    Properties p=new Properties();
    p.setProperty("username","cxh1005");
    p.setProperty("sex","女");
    p.setProperty("schoolNum","201609080");
    try{
    PrintStream fw=new PrintStream("/Users/cxh/IdeaProjects/JavaBaseTest/src/bin/printStream.properties");
    p.list(fw);//将内存中内容写入到fw的目录文件中
    }catch (IOException e){
    e.printStackTrace();
    }
    }
    }

result:

Then, we will find that the content order in the production file is different from what we set. Because the properties class implements the map interface, it uses key value to store data, and the data is not guaranteed in order.

/**

  • Created by cxh on 17/07/21.
    */
    public class Main {
    public static void main(String[] args) {
    Properties p=new Properties();
    p.setProperty("IP","127.0.0.1");
    p.setProperty("Mac","98:73:41:ac:0f:c2");
    try{
    PrintStream ps=new PrintStream(new File("/Users/cxh/IdeaProjects/JavaBaseTest/src/bin/printStreams.xml"));
    p.storeToXML(ps,"testPrintToXMLFile");
    }catch(IOException e){
    e.printStackTrace();
    }
    }
    }

Test results:


stem "http://java.sun.com/dtd/properties.dtd">

  
  
  

   
   
   
    
    
    testPrintToXMLFile
   
   
   

   
   
   

    
    
    

    
    
    
   
   
   
  
  
  

stem "http://java.sun.com/dtd/properties.dtd">

  
  
  
    
   
   
   
    
    
    testPrintReadFromXMLFile
   
   
   
    
   
   
   
    
    
    
    

    
    
    
   
   
   
  
  
  

3.2. 2. Read < span style = "font size: 14px;" > readSteam. Contents in XML file:

/**

  • Created by cxh on 17/07/21.
    */
    public class Main {
    public static void main(String[] args) throws Exception{
    Properties p=new Properties();
    FileInputStream fis=new FileInputStream("/Users/cxh/IdeaProjects/JavaBaseTest/src/bin/readStream.xml");
    p.loadFromXML(fis);
    p.list(System.out);
    System.out.println(); //空行
    System.out.println("the Mac property is :"+p.getProperty("Mac"));
    }
    }

Output content:

the Mac property is :98:73:41:ac:0f:c2

Process finished with exit code 0

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>