Chapter 1 attribute file operation tool class, Chapter 1 tool class
1. Code Implementation
Property file: http. properties (located under the class path)
1 # maximum number of connections per Route 2 httpclient. max. conn. per. route = 20 3 # maximum number of connections 4 httpclient. max. conn. total = 4005 # connection timeout (MS) 6 httpclient. max. conn. timeout = 10007 # operation time-out (MS) 8 httpclient. max. socket. timeout = 1000View Code
Note: The default Attribute Editor of eclipse cannot display Chinese characters. Install the plug-in (eclipse -- propedit_5.3.3.
Attribute file operation tool: FileUtil
1 package com. util; 2 3 import java. io. IOException; 4 import java. io. inputStream; 5 import java. util. properties; 6 7 import org. apache. commons. lang. math. numberUtils; 8 9/** 10 * file operation tool class 11 */12 public class FileUtil {13 14/** 15 * load attribute file *. properties16 * @ param fileName is not the full path name of the attribute, but relative to the class path name 17 */18 public static Properties loadProps (String fileName) {19 Properties props = null; 20 InputStream is = null; 21 22 try {23 is = Thread. currentThread (). getContextClassLoader (). getResourceAsStream (fileName); // get the fileName file under the class path and convert it to the input stream 24 if (is! = Null) {25 props = new Properties (); 26 props. load (is); // load attribute file 27} 28} catch (Exception e) {29 e. printStackTrace (); 30} finally {31 if (is! = Null) {32 try {33 is. close (); 34} catch (IOException e) {35 e. printStackTrace (); 36} 37} 38} 39 40 return props; 41} 42 43/* 44 * only lists the methods for getting int data from the attribute file, methods for obtaining other types are similar to 45 */46 public static int getInt (Properties props, String key, int defaultValue) {47 int value = defaultValue; 48 49 if (props. containsKey (key) {// whether the attribute file contains the specified key value 50 value = NumberUtils. toInt (props. getProperty (key), defaultValue); // extract the value of the given key value from the attribute file and convert it to int type 51} 52 53 return value; 54} 55 56/** 57 * test 58 */59 public static void main (String [] args) {60 Properties props = FileUtil. loadProps ("http. properties "); 61 System. out. println (FileUtil. getInt (props, "httpclient. max. conn. per. route ", 10); // This key62 System is in the property file. out. println (FileUtil. getInt (props, "httpclient. max. conn. per. route2 ", 10); // This key63} 64} is not in the property file}View Code
Note:
- Method for reading files from a class path: Thread. currentThread (). getContextClassLoader (). getResourceAsStream (fileName );
- GetInt () method: org. apache. commons. lang. math. the toInt (String str, int defaultValue) method of the NumberUtils class, observe the source code 1 public static int toInt (String str, int defaultValue) {2 if (str = null) {3 return defaultValue; 4} 5 try {6 return Integer. parseInt (str); 7} catch (NumberFormatException nfe) {8 return defaultValue; // toInt ("hello", 123) 9} 10}View Code
Execution Process: If the input str is null, defaultValue is directly returned; otherwise, Integer is used. parseInt (str) converts str to int type. If the conversion is successful (eg. str = "123") directly returns the converted int type (eg.123). If the conversion fails (eg. str = "hello"), return defaultValue directly.Note: This tool class is worth learning.