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