Java read/write and modify the property File

Source: Internet
Author: User

Import java. Io. bufferedinputstream;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import java. util. properties;
/**
*
*/

/**
* @ Author Lenovo
*
*/
Public class acesstest {
 
Private Static final string property_file = "F:/Program/data. properties ";
 
/**
25. * read value based on key
26 .*
27. * @ Param key
28. * @ return
29 .*/
Public String readdata (string key ){
Properties props = new properties ();
Try {
Inputstream in = new bufferedinputstream (New fileinputstream (
Property_file ));
Props. Load (in );
In. Close ();
String value = props. getproperty (key );
Return value;
} Catch (exception e ){
E. printstacktrace ();
Return NULL;
}
}


/**
. * Modify or add a key-value pair if the key exists. Otherwise, add the key.
48 .*
49. * @ Param key
50. * @ Param Value
51 .*/
Public void writedata (string key, string value ){
Properties prop = new properties ();
Try {
File file = new file (property_file );
If (! File. exists ())
File. createnewfile ();
Inputstream FCM = new fileinputstream (File );
Prop. Load (FS );
FCM. Close (); // You must disable the function before modifying the value.
Outputstream Fos = new fileoutputstream (property_file );
Prop. setproperty (Key, value );
Prop. Store (FOS, "Update'" + key + "'value ");
Prop. Store (FOS, "just for test ");

FOS. Close ();
} Catch (ioexception e ){
System. Err. println ("visit" + property_file + "for updating"
+ Value + "value error ");
}
}

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Acesstest test = new acesstest ();
Test. writedata ("name", "Xiaozhang ");
Test. writedata ("sex", "male ");

// Test. writedata ("name", "Xiaoyang ");

String name = test. readdata ("name ");
String sex = test. readdata ("sex ");
System. Out. println ("the name of the person is:" + name + ", and the sex is:" + sex );
}

}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/zymm3221/archive/2010/10/11/5932400.aspx

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.