Properties [key details]
1, Introduction & overview
Why use Properties: to save the configuration information of the program more professionally.
Configuration information: information required for program running Program A: User name = zhangsan Password =xxxx IP=192.168.36.11 Authority = administrator Code table =utf-8
The Properties class represents a persistent property set, which can store key value pairs (hashmaps) like the Map set
Persistent: save data from memory to hard disk All Key value s are strings |
---|
2, Entry case: adding key value pairs
Properties persist data on the hard disk.
It's equivalent to HashMap, unordered, with key value. Both key and value are strings
/* * Getting started: adding key value pairs to Properties * HashMap * */ @Test public void run1(){ //1. The key value of the created object is String Properties p = new Properties(); //2. Add key value pair //2.1. Add key value pair: setProperty(key,value) is equivalent to map put (key, value), with the same effect p.setProperty("k1","v1"); p.setProperty("k2","v2"); p.setProperty("k2","v3"); //2.2. Get key value pair: getProperty(key) is equivalent to map get (key), with the same effect String k2 = p.getProperty("k2"); System.out.println(k2); System.out.println(p); //3, traversal //3.1. Turn Properties into Set set Set. All key s are saved in the Set set Set<String> set = p.stringPropertyNames(); //3.2 traverse Set set for (String key : set) { System.out.println(key+"----------"+p.getProperty(key)); } }
3, Advanced case 1: write data operation byte stream
To write out the configuration file
/* * Save the key value pair to Properties and write it out as a configuration file * Byte: Universal * character * */ @Test public void run1() throws Exception { //1. Create the Properties object and save the data Properties p = new Properties(); p.setProperty("name","Eldest brother"); p.setProperty("sex","Pure men"); p.setProperty("age","18"); //2. Create output stream object extension must be:. properties FileOutputStream fos = new FileOutputStream("pd.properties"); //3. Write out file: store store p.store(fos,"This is the big brother's message"); //4, Guan Liu fos.close(); }
4, Advanced case 2: read data operation byte stream
To read the configuration file
/* * Read data from Properties file * */ @Test public void run2() throws Exception { //1. Create Properties object (ready to read data) Properties p = new Properties(); //2. To create an input stream, the read file extension must be:. properties FileInputStream fis = new FileInputStream("pd.properties"); //3. Read load / read p.load(fis); System.out.println(p); //4, Guan Liu fis.close(); }
5, Advanced case 3: byte stream character stream comparison
Write in byte stream:
/* * Read with character stream * */ @Test public void run3() throws Exception { //1. Create Properties object (ready to read data) Properties p = new Properties(); //2. To create an input stream, the read file extension must be:. properties FileReader fr = new FileReader("Byte stream.properties"); //3. Read load / read p.load(fr); System.out.println(p); //4, Guan Liu fr.close(); } /* * Read with byte stream * */ @Test public void run2() throws Exception { //1. Create Properties object (ready to read data) Properties p = new Properties(); //2. To create an input stream, the read file extension must be:. properties FileInputStream fis = new FileInputStream("Byte stream.properties"); //3. Read load / read p.load(fis); System.out.println(p); //4, Guan Liu fis.close(); } /* * Write in byte stream * */ @Test public void run1() throws Exception { //1. Create the Properties object and save the data Properties p = new Properties(); p.setProperty("name","Eldest brother"); p.setProperty("sex","Pure men"); //2. Create output stream object extension must be:. properties FileOutputStream fos = new FileOutputStream("Byte stream.properties"); //3. Write out file: store store p.store(fos,"This is the big brother's message"); //4, Guan Liu fos.close(); }
Write in character stream:
/* * Read with character stream * */ @Test public void run3() throws Exception { //1. Create Properties object (ready to read data) Properties p = new Properties(); //2. To create an input stream, the read file extension must be:. properties FileReader fr = new FileReader("Character stream.properties"); //3. Read load / read p.load(fr); System.out.println(p); //4, Guan Liu fr.close(); } /* * Read with byte stream * */ @Test public void run2() throws Exception { //1. Create Properties object (ready to read data) Properties p = new Properties(); //2. To create an input stream, the read file extension must be:. properties FileInputStream fis = new FileInputStream("Character stream.properties"); //3. Read load / read p.load(fis); System.out.println(p); //4, Guan Liu fis.close(); } /* * Write in character stream * */ @Test public void run1() throws Exception { //1. Create the Properties object and save the data Properties p = new Properties(); p.setProperty("name","Eldest brother"); p.setProperty("sex","Pure men"); //2. Create output stream object extension must be:. properties FileWriter fw = new FileWriter("Character stream.properties"); //3. Write out file: store store p.store(fw,"This is the big brother's message"); //4, Guan Liu fw.close(); }
A file written out by a byte stream that can be read by either a byte stream or a character stream
The file written out by character stream can only be read by character stream (byte stream scrambling)
Recommendation: use byte stream to read and write
6, IDEA creates Properties file
Today's summary:
Properties: a collection of persisted data. (HashMap)Persistence: save memory data to hard disk.Both Key and value are String stringssetProperties(key,value); map.put(key,value)getProperties(key); map.get(key)stringPropertyNames(); map.keySet();Output stream: extension.propertiesStore (output stream object, description information);Input streamLoad (input stream object)Byte stream: write, any stream can read [suggestions]Character stream: write out, only character stream |
---|
Please give yourself a compliment!
Make a little progress every day`~~~~~