First, Java Properties class
Java has a relatively important class properties (Java.util.Properties), mainly used to read Java configuration files, various languages have their own supported configuration files, many variables in the configuration file is often changed, and this is to facilitate users, so that users can be removed from the program itself to modify The associated variable settings. Like a python-supported profile is an. ini file, it also has its own class configparse that reads the configuration file, allowing programmers or users to modify the. ini configuration file by means of this class. In Java, its configuration file is usually a. properties file, formatted as a text file, the format of the contents of the file is "key = value" format, the text annotation information can be used "#" to comment.
The properties class inherits from Hashtable, as follows:
It provides several main methods:
1. GetProperty (String key) that searches for properties in this property list with the specified key. That is, by the parameter key, the value corresponding to the key is obtained.
2. Load (InputStream instream) to read the list of attributes (key and element pairs) from the input stream. Gets all the key-value pairs in the file by loading the specified file (for example, the Test.properties file above). To search for GetProperty (String key).
3. SetProperty (string key, String value), calls the Hashtable method put. He sets a key-value pair by calling the put method of the base class.
4. Store (OutputStream out, String comments) writes the list of attributes (key and element pairs) in this properties table to the output stream, in a format that is appropriate for loading into the properties table using the Load method. In contrast to the load method, the method writes a key-value pair to the specified file.
5. Clear () Clears all loaded key-value pairs. This method is provided in the base class.
Second, Java read the properties file
The most common is implemented by the Java.lang.Class class's getResourceAsStream (String name) method, which can be called as follows:
InputStream in = GetClass (). getResourceAsStream ("Resource Name"); As we write the program, use this one enough.
Or the following is commonly used:
InputStream in = new Bufferedinputstream (new FileInputStream (filepath));
Third, classroom practice
1.
Packageday042301;Importjava.util.Properties;ImportJava.util.Set; Public classDemo1 { Public Static voidMain (string[] args) {Properties Pro=NewProperties (); Pro.setproperty ("A", "1"); Pro.setproperty ("B", "2"); Pro.setproperty ("C", "3"); Pro.setproperty ("D", "4"); String VL=pro.getproperty ("D"); SYSTEM.OUT.PRINTLN (VL); //iterate through the collection to get to the set set containing the key valuesSet<string> s=Pro.stringpropertynames (); for(String str:s) {//get the corresponding value by traversing the keyString key=str; String value=Pro.getproperty (key); SYSTEM.OUT.PRINTLN (Key+"..."+value); } }}
Run results
2.
Packageday042301;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.util.Iterator;Importjava.util.Properties;ImportJava.util.Set; Public classDemo2 { Public Static voidMain (string[] args)throwsIOException {input (); } Public Static voidOutput ()throwsioexception{Properties Pro=NewProperties (); Pro.setproperty ("Li Baolin", "Eat takeout"); Pro.setproperty ("Li Baolin second", "Eat a big meal"); Pro.setproperty ("Li Baolin No. Third", "Snack"); FileWriter FW=NewFileWriter ("D:\\test\\demo.properties"); Pro.store (FW,""); Fw.close ();}//Output Public Static voidInput ()throwsioexception{Properties Pro=NewProperties (); FileReader FR=NewFileReader ("D:\\test\\demo.properties"); Pro.load (FR); Fr.close (); Set<String> set=Pro.stringpropertynames (); Iterator<String> it=Set.iterator (); while(It.hasnext ()) {String key=It.next (); String value=Pro.getproperty (key); SYSTEM.OUT.PRINTLN (Key+"..."+value); }}}
Run results
3.
Packageday042303;ImportJava.io.File;Importjava.io.IOException;Importorg.apache.commons.io.FileUtils;Importorg.apache.commons.io.FilenameUtils; Public classFileutildemo { Public Static voidMain (string[] args)throwsIOException {filenameutil ();//Get extension } Public Static voidFilenameutil ()throwsioexception{filenameutils fnu=Newfilenameutils (); //Get extensionString name1=fnu.getextension ("D:\\test\\person.txt"); //Get file nameString name2=fnu.getname ("D:\\test\\person.txt"); //To determine the suffix name BooleanFlag=fnu.isextension ("D:\\test\\person.txt", "TXT"); System.out.println (name1+ "" +name2+ "" +flag); //Read File contentsFileUtils fu=NewFileUtils (); String content=fu.readfiletostring (NewFile ("D:\\test\\person.txt")); SYSTEM.OUT.PRINTLN (content); //Fu.writestringtofile (New File ("D:\\test\\today.txt"), "it rained Today"); //fu.copydirectorytodirectory (New file ("D:\\test"), New file ("D:\\java")); //Copying FilesFu.copyfile (NewFile ("D:\\test\\today.txt"),NewFile ("D:\\java\\ttttt.txt")); }}
Run results
4.
Packageday042303;ImportJava.io.BufferedReader;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.PrintWriter;/*Print Stream * 1.printwriter (file,output,string,writer) (auto refresh), more flexibility * *2.printstream * Features: * 1. This stream is not responsible for the data source, only the data purposes. Add features to other output streams. Never throw a ioexception.*/ Public classPrintdemo { Public Static voidMain (string[] args)throwsioexception {copy (); } Public Static voidMethod1 ()throwsfilenotfoundexception{File File=NewFile ("D:\\test\\person.txt"); PrintWriter PW=Newprintwriter (file); Pw.println ("100"); Pw.flush (); Pw.write (100); Pw.close (); } Public Static voidmethod2 () {int[] Arr={1}; System.out.println (arr); Char[] ch={' A ', ' B ', ' C '}; SYSTEM.OUT.PRINTLN (CH); } Public Static voidMETHOD3 ()throwsfilenotfoundexception{//automatic refresh feature for print streams//condition: 1. Your output data purpose must be a stream object//2. Methods that must be called: Println,printf,format one of theFile file=NewFile ("D:\\test\\person.txt"); FileOutputStream Fos=Newfileoutputstream (file); PrintWriter PW=NewPrintWriter (FOS,true); Pw.println ("51 where to go to play"); Pw.println ("Where to play on the first day"); Pw.println ("Write code."); Pw.close (); }//Copy Public Static voidCopy ()throwsioexception{//read data with Budderedreader+filereader//write to destination: Printwriter+println Auto Refresh functionBufferedReader br=NewBufferedReader (NewFileReader ("D:\\test\\person.txt")); PrintWriter PW=NewPrintWriter (NewFileWriter ("D:\\test\\aaaa\\hello.txt"),true); String Line=NULL; while((Line=br.readline ())! =NULL) {pw.println (line); } pw.close (); Br.close (); }}
April 23, 2018 Java