Any programming language has its own method and format for reading and writing configuration files, and Java is no exception.
The most important class for reading and writing resource files in the Java programming language is the properties, which are generally as follows:
1. Read and Write properties file 2. Read and write XML file 3. Not only can read and write the above two types of files, but also read and write other format files such as TXT, as long as the Key=value format can be. Note: The resource file contains Chinese when processing Method 1. Convert Chinese characters through work to UTF8 encoding, either through Java's own nativetoascii or the property editor in Eclipse. 2. Call the new String directly (Youchinesestring.getbytes ("iso-8859-1"), "GBK"); Attached: Method of loading resource file in Web program properties prop = null; 1. Prop = Thread.CurrentThread (). Getcontextclassloader (). getResourceAsStream ("filename"); 2. Prop = This.getclass (). getClassLoader (). getResourceAsStream ("filename"); The properties class inherits from Hashtable, and the approximate API is as follows: OK, just talk to the code, this class is easy to use to see the demo directory structure: First, read the configuration file class: Propertiesreader.java about properties read files Here are six ways: Java reads the properties of six ways, the following is the most common one About the way the path is written: (You can also have a relative path or an absolute path) Class.getresourceasstream (String path)
Path does not start with '/' by default when the resource is fetched from the package under which the class is located, and starts with '/' from the root of the classpath (src file).
1 Packagecom.lcw.properties.test;2 3 Importjava.io.IOException;4 ImportJava.io.InputStream;5 Importjava.util.Enumeration;6 Importjava.util.Properties;7 8 /**9 * Properties file Read classTen * One */ A Public classPropertiesreader { - - Public voidGetpropertiesreader () { theProperties properties=NewProperties ();//get instance of properties -InputStream Instream=getclass (). getResourceAsStream ("config.properties");//get configuration file input stream - Try { -Properties.load (instream);//Load Input stream +Enumeration Enumeration=properties.propertynames ();//get all the key values in the config file - while(Enumeration.hasmoreelements ()) { +String key=(String) enumeration.nextelement (); ASYSTEM.OUT.PRINTLN ("Key value in config file:" +key+ "value in =====> Config file:" +properties.getproperty (Key) ");//Output Key value at } - -}Catch(IOException e) { - e.printstacktrace (); - } - } in -}
One more Test class: Propertiestest.java
1 Packagecom.lcw.properties.test;2 3 Public classPropertiestest {4 5 /**6 * Test Class7 */8 Public Static voidMain (string[] args) {9Propertiesreader propertiesreader=NewPropertiesreader ();Ten Propertiesreader.getpropertiesreader (); One } A -}
This is the profile information: config.properties
color=blackanimal=Rabbitfood=Hamburgerchinese=\u6211\u662f\u4e2d\u6587
Look at the effect of the operation:
To write to the configuration I file is also simple, here is a method to add:
1 //Write resource file information2 Public voidwriteproperties () {3Properties properties=NewProperties ();4 Try {5OutputStream outputstream=NewFileOutputStream ("Config.properties");6Properties.setproperty ("Number", "2015");7Properties.setproperty ("Song", "the past of handwriting");8Properties.store (OutputStream, "Rabbit");9 outputstream.close ();Ten}Catch(FileNotFoundException e) { One e.printstacktrace (); A}Catch(IOException e) { - e.printstacktrace (); - } the}
Generate File:
17:16:56 CST number =2015song=\u6211\u7231\u4f60
Brief usage Notes for Java read-write configuration file--properties class