This tool class is convenient for configuring file reads in Java Projects ~
Example code:
PackageCom.lky.util;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;Importjava.util.Properties;ImportOrg.apache.commons.logging.Log;Importorg.apache.commons.logging.LogFactory;/*** @ClassName: readconfigutil* @Description: Read Profile Tool Class (Properties type and file type) *@authorlky* @date October 26, 2015 *@version1.0*/ Public classReadconfigutil {Private StaticLog log = Logfactory.getlog (readconfigutil.class); PrivateInputStream InputStream; PrivateBufferedReader Breader; PrivateProperties config; PrivateString strline; PublicString Getstrline () {returnstrline; } Public voidsetstrline (String strline) { This. strline =strline; } /*** @describe Constructor *@paramFileName *@paramflag True indicates that a file of the properties type is read, otherwise the TXT file*/ PublicReadconfigutil (String FileName,Booleanflag) {Config=NULL; Breader=NULL; InputStream= Readconfigutil.class. getClassLoader (). getResourceAsStream (FileName); Try { if(flag) {config=NewProperties (); Config.load (InputStream); } Else{Breader=NewBufferedReader (NewInputStreamReader (InputStream, "Utf-8")); Setstrline (ReadFile ()); } } Catch(IOException e) {e.printstacktrace (); } finally { Try { if(Breader! =NULL) {breader.close (); } if(InputStream! =NULL) {inputstream.close (); } } Catch(IOException e) {e.printstacktrace (); } } } Publicstring GetValue (String key) {returnConfig.getproperty (key); } PrivateString ReadFile () {StringBuffer sbuffer=NewStringBuffer (); Try{String line=NULL; while(line = Breader.readline ())! =NULL) { if(Line.trim (). Length () > 0 && (!line.trim (). StartsWith ("#")) {sbuffer.append (line); Sbuffer.append ("\ n"); } } } Catch(Exception e) {log.info ("Read file Exception!!!!" "); } returnsbuffer.tostring (); } Public Static voidMain (String args[]) {System.out.println (NewReadconfigutil ("Sava.txt",false). Getstrline ()); System.out.println (NewReadconfigutil ("Test.properties",true). GetValue ("IP")); System.out.println (NewReadconfigutil ("Test.properties",true). GetValue ("Uame")); System.out.println (NewReadconfigutil ("Test.properties",true). GetValue ("Password")); }}
Java--configuration file read