Properutil. Java attribute File Operations

Source: Internet
Author: User
Import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. printstream; import Java. util. properties; public class properutil {private properties propertie; private fileinputstream inputfile; private fileoutputstream outputfile; Public properutil () {This. propertie = new properties ();} public properutil (string filepath) {thi S. propertie = new properties (); try {This. inputfile = new fileinputstream (filepath); this. propertie. load (this. inputfile); this. inputfile. close ();} catch (filenotfoundexception ex) {system. out. println ("failed to read attribute file --->! -Cause: the file path is incorrect or the file does not exist "); Ex. printstacktrace ();} catch (ioexception ex) {system. Out. println (" loading file ---> failed! "); Ex. printstacktrace () ;}} Public String getvalue (string key) {If (this. propertie. containskey (key) {string value = This. propertie. getproperty (key); return value;} return "";} Public String getvalue (string filename, string key) {try {string value = ""; this. inputfile = new fileinputstream (filename); this. propertie. load (this. inputfile); string str1; If (this. propertie. containskey (key) {value = This. propertie. getproperty (key); str1 = value; return str1;} return value;} catch (filenotfoundexception e) {e. printstacktrace (); Return "";} catch (ioexception e) {e. printstacktrace (); Return "";} catch (exception ex) {ex. printstacktrace (); Return "" ;}finally {try {This. inputfile. close ();} catch (ioexception e) {e. printstacktrace () ;}} public void clear () {This. propertie. clear ();} public void setvalue (string key, string value) {This. propertie. setproperty (Key, value);} public void SaveFile (string filename, string description) {try {This. outputfile = new fileoutputstream (filename); this. propertie. store (this. outputfile, description);} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception IOE) {IOE. printstacktrace ();} finally {try {This. outputfile. close ();} catch (ioexception e) {e. printstacktrace () ;}} public properties getpropertie () {return this. propertie;} public static void main (string [] ARGs ){}}

 

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.