Java删除properties配置文件中指定键值
public static boolean deleteKeyValue4Pro(String delete_key, String delete_value) { boolean flag = false; String toreplace =delete_key + "=" + delete_value + "\n"; try { StringBuffer sb = new StringBuffer(); String templine; File file = new File("ftpconfig.properties"); BufferedReader bin = new BufferedReader(new FileReader(file)); while ((templine = bin.readLine()) != null) { templine = unicodeToString(templine); sb.append(templine + "\n"); } String save = new String(sb.toString()); System.out.println("before delete:\n" + save + "\n--------------\n"); System.out.println("delete:" + toreplace + "\n--------------\n"); Pattern pattern = Pattern.compile(toreplace, Pattern.MULTILINE); Matcher matcher = pattern.matcher(save); while (matcher.find()) { save = matcher.replaceAll(""); } BufferedWriter writer = new BufferedWriter(new FileWriter(file)); String[] saves = save.split("\n"); for (int i = 0; i < saves.length; i++) { String[] key_values = saves[i].split("="); writer.write(stringToUnicode(key_values[0]) + "=" + key_values[1] + "\n"); } System.out.println("after delete:\n" + save); writer.flush(); bin.close(); writer.close(); flag = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return flag; }