Java small data storage, resource file read/write example

Source: Internet
Author: User

/*
* Function Description: reads the value corresponding to a key of the resource file.
* @ Param key refers to the corresponding key in the resource file; for example, com.cn. Test
* @ Param file_path: the path of the resource file, which can be an absolute path. For example,/test. properties.
*/
Private Static string readproperties (string key, string file_path ){
Properties props = new properties ();

Inputstream instream = Main. Class. getresourceasstream (file_path );

Try {
Props. Load (instream );
Instream. Close ();
} Catch (ioexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
}

Return props. getproperty (key );
}

/*
* Function Description: write data to the resource file. Note that the resource file must be placed in the SRC and directory.
* @ Param key refers to the corresponding key in the resource file; for example, com.cn. Test
* @ Param value the value corresponding to the key in the resource file;
* @ Param file_name Note: The resource file must be placed in the SRC and directory. For example, test. properties.
*/
Public static void SaveFile (string key, string value, string file_name ){
String filepath = Main. Class. getresource ("/"). getpath () + file_name;
File file = new file (filepath );
Inputstream FCM = NULL;
Outputstream outputfile = NULL;
Properties props = new properties ();
Try {
FCM = new fileinputstream (filepath );
// Read the attribute list (key and element pair) from the input stream)
Props. Load (FS );
Outputfile = new fileoutputstream (File );
Props. setproperty (Key, value );
// Write the attribute list (key and element pair) in the properties table to the output stream.
Props. Store (outputfile, "Update'" + value + "'value ");
Outputfile. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}

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.