The properties file used in Java is a more common configuration file for configuring Key-value, and Java has a class that specifically handles that type of file.
First, the Properties class API
1. Properties Class Description
Properties inherits the Hashtable, which explicitly indicates that it is a collection of Key-value, which describes the use of the GET, put method, but guarantees that the Key-value is a string, otherwise there is an error in writing the file. It is safe to operate with its own getproperty and SetProperty.
2. Properties Method Description
The description of the method is relatively easy to understand, consisting primarily of the load input stream (parsing the properties of the Key-value), the key-value pair collection operation, and the writing of the collection contents to the output stream (store, list)
Second, the properties class preliminary use
1. To read the contents of the properties file into the Properties collection first to load, here are three methods (character stream, byte stream, and XML load), then the process is this:
File stream "load
Public classtestproperties { Public Static voidMain (string[] args)throwsIOException {File f=NewFile (NewTestproperties (). GetClass (). GetResource ("Request-mapping.properties"). GetPath ()); //byte stream ProcessingInputStream is =NewFileInputStream (f); Properties P=NewProperties (); P.load (IS); System.out.println (P.getproperty ("/nutrition/food/foodclassificationcontroller")); Is.close (); //character Stream ProcessingFileReader FR =NewFileReader (f); Properties P2=NewProperties (); P2.load (FR); System.out.println (P2.getproperty ("/nutrition/food/foodclassificationcontroller")); Fr.close (); }}View Code
Request-mapping.properties file contents are as follows
/nutrition/food/foodclassificationcontroller = Cn.nutrition.food.controller.FoodClassificationController
Results The values of Cn.nutrition.food.controller.FoodClassificationController are printed in both ways
2, the properties of the contents of the file is also written in several ways the list of two file streams (no limit to the load stream type), the store corresponding to the two file streams (must be the same as the load stream to operate successfully), which can be used in combination
New File (new testproperties (). GetClass (). GetResource ("Request-mapping.properties"). GetPath ()); // byte stream processing New FileInputStream (f); New Properties (); P.load (is); System.out.println (P.getproperty ("/nutrition/food/foodclassificationcontroller")); Is.close (); P.setproperty ("key", "value"); New FileOutputStream (f); P.store (OS,"2018.5.10");
View Code
File operation result content:
#2018.5.1000:27:29 CST 2018/nutrition/food/foodclassificationcontroller= Cn.nutrition.food.controller.FoodClassificationControllerkey=value
In addition to the key-value pairs, there are two lines of "#" marked comment content
Look at another way to write:
New File (new testproperties (). GetClass (). GetResource ("Request-mapping.properties"). GetPath ()); // byte stream processing New FileInputStream (f); New Properties (); P.load (is); System.out.println (P.getproperty ("/nutrition/food/foodclassificationcontroller")); Is.close (); P.setproperty ("key", "value"); New PrintWriter (f); P.store (OS,"2018.5.10");
View Code
File operation results as above, seemingly with the API inside the description there is a difference, the specific reason for a moment to put the first
The Third Way:
file F = new File (new testproperties ( ). GetClass (). GetResource ("request-mapping.properties" // byte throttle processing InputStream is = new FileInputStream (f); Properties P = new Properties (); P.load (IS); System.out.println (P.getproperty ( "/nutrition/food/foodclassificationcontroller" "key", "value" = new PrintWriter (f); P.list (OS); Os.flush (); Os.close ();
View Code
Note that the OS here needs to call flush.
Make more than a few operations results:
--Listing properties----=listing Properties--/nutrition/food/foodclassificationcontroller= Cn.nutrition.food.controller.foodclas...key=value
This proves that this method is only suitable for use as a log printing list (System.out), can not write files in this way, otherwise it is easy to cause data loss.
Third, the properties file operation of the tool package, where the package is only for the properties file read operation
PackageCn.nutrition.common.util;Importjava.io.Closeable;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Map.Entry;Importjava.util.Properties;ImportJava.util.Set; Public classPORPERTIESUITL { Public StaticProperties GetProperties (String filePath) {Properties Properties=NewProperties (); File F=NewFile (porpertiesuitl.class. GetResource ("Request-mapping.properties"). GetPath ()); InputStream is=NULL; Try{ is=NewFileInputStream (f); Properties.load (IS); if(NULL!=is ) {Is.close (); } } Catch(IOException e) {e.printstacktrace (); } finally{Closeio (IS); } returnproperties; } Public StaticMap<string, string>getmapping (String filePath) {Map<string, string> map =NewHashmap<string, string>(); Properties Properties=getProperties (FilePath); Set<entry<object, object>> Entrys =Properties.entryset (); for(Entry<object, object>Entry:entrys) {Map.put (Entry.getkey (). ToString (), Entry.getvalue (). ToString ()); } returnmap; } Private Static voidCloseio (closeable io) {if(NULL!=io) { Try{io.close (); } Catch(IOException e) {e.printstacktrace (); } } }}View Code
This involves the file loading path problem under the source directory, refer to
Loading of XML files
"001" Operation of configuration file properties in Java