Here you can find the source of setPropertiesVaule(File file, String key, String value, String comments, Charset charset)
Parameter | Description |
---|---|
file | the file |
key | the key |
value | the value |
comments | the comments |
charset | the charset |
Parameter | Description |
---|---|
IOException | the io exception |
public static String setPropertiesVaule(File file, String key, String value, String comments, Charset charset) throws IOException
//package com.java2s; //License from project: Apache License import java.io.*; import java.nio.charset.Charset; import java.util.Date; import java.util.Iterator; import java.util.Properties; import java.util.Set; public class Main { /**/*from w w w.j a v a 2 s . c o m*/ * Sets properties vaule. * * @param file the file * @param key the key * @param value the value * @return String properties vaule * @throws IOException the io exception * @Title: setPropertiesVaule * @Description: */ public static String setPropertiesVaule(File file, String key, String value) throws IOException { return setPropertiesVaule(file, key, value, file.getPath()); } /** * Sets properties vaule. * * @param file the file * @param key the key * @param value the value * @param comments the comments * @return String properties vaule * @throws IOException the io exception * @Title: setPropertiesVaule * @Description: */ public static String setPropertiesVaule(File file, String key, String value, String comments) throws IOException { return setPropertiesVaule(file, key, value, comments, null); } /** * Sets properties vaule. * * @param file the file * @param key the key * @param value the value * @param comments the comments * @param charset the charset * @return String properties vaule * @throws IOException the io exception * @Title: setPropertiesVaule * @Description: */ public static String setPropertiesVaule(File file, String key, String value, String comments, Charset charset) throws IOException { if (file == null) { return null; } if (!file.exists()) { file.createNewFile(); } if (charset == null) { charset = Charset.forName("ISO8859-1"); } Properties properties = new Properties(); FileInputStream fis = null; try { fis = new FileInputStream(file); properties.load(fis); } catch (IOException e) { throw new IOException(e); } finally { if (fis != null) { fis.close(); } } Object v = properties.setProperty(new String(key.getBytes(charset), "ISO8859-1"), new String(value.getBytes(charset), "ISO8859-1")); BufferedWriter bw = null; try { Set set = properties.entrySet(); Iterator<Object> iterator = set.iterator(); bw = new BufferedWriter(new FileWriter(file)); bw.write("#" + comments); bw.newLine(); bw.write("#" + new Date().toString()); bw.newLine(); while (iterator.hasNext()) { String str = iterator.next().toString(); bw.write(new String(str.getBytes("ISO8859-1"), charset)); bw.newLine(); } bw.flush(); } catch (FileNotFoundException e) { throw new IOException(e); } finally { if (bw != null) { bw.close(); } if (properties != null) { properties.clear(); } } return v == null ? null : new String(v.toString().getBytes("ISO8859-1"), charset); } }