Package com. hzk. utils; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. io. unsupportedEncodingException; import java. util. enumeration; import java. util. properties; public class PropertiesTools {public static void writeProperties (String filePath, String parameterName, String parameterValue) {Properties props = new Properties (); try {File f = new File (filePath ); if (f. exists () {InputStream FCM = new FileInputStream (filePath); props. load. close ();} else {System. out. println (filePath); f. createNewFile ();} OutputStream fos = new FileOutputStream (filePath); props. setProperty (parameterName, parameterValue); props. store (fos, ""); fos. close ();} catch (IOException e) {e. printStackTrace () ;}} public static Properties readProperties (String filePath) {Properties props = new Properties (); InputStream is; try {is = new FileInputStream (filePath); props. load (is); is. close (); return props;} catch (Exception e1) {e1.printStackTrace (); return null ;}/ *** convert the encoding into a iso-8859-1 before writing ,. propertise default encoding * @ param data * @ return */public static String iso2utf8 (String data) {String result = ""; try {result = new String (data. getBytes ("iso-8859-1"), "UTF-8");} catch (UnsupportedEncodingException e) {e. printStackTrace ();} return result;}/*** transcode the data to UTF-8 for easy reading * @ param data * @ return */public static String utf82iso (String data) {String result = null; try {result = new String (data. getBytes ("UTF-8"), "iso-8859-1");} catch (UnsupportedEncodingException e) {e. printStackTrace ();} return result;} public static void main (String [] args) {PropertiesTools. writeProperties ("d: \ datas. properties ", utf82iso (" name "), utf82iso (" tom "); PropertiesTools. writeProperties ("d: \ datas. properties ", utf82iso (" "), utf82iso (" hzk "); PropertiesTools. writeProperties ("d: \ datas. properties ", utf82iso (" hk "), utf82iso (" account "); Properties props = PropertiesTools. readProperties ("d :\\ datas. properties "); Enumeration en = props. keys (); while (en. hasMoreElements () {String key = (String) en. nextElement (); String keyDecode = iso2utf8 (key); String value = iso2utf8 (String) props. getProperty (key); System. out. println ("key:" + keyDecode + "value:" + value );}}}
As shown in the code above, note that the default encoding of the new properties file is iso-8859-1, so to read and write Chinese data, must be transcoded, for Chinese will be displayed as a form, see datas. properties:
#
# Sat Jun 14 15:38:10 CST 2014
Hk = \ u00E6 \ u0088 \ u00B7 \ u00E5 \ u008F \ u00A3
Name = tom
\ U00E5 \ u00A5 \ u00BD \ u00E8 \ u00BF \ u0099 \ u00E5 \ u008F \ u00A3 = hzk
If in myeclipse Save As UTF-8 form, once again can manually enter Chinese can, but the next time the Code is written and then open will become iso-8859-1 garbled, It is a pain, therefore, if you want to read Chinese characters, you can convert them to UTF-8 through code reading, or save them in UTF-8 format and edit Chinese characters. Do not write the code into Chinese.