Java--configuration file read

Source: Internet
Author: User
Tags log log readfile

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.