Here you can find the source of searchReplace(String key, String value, List
public static boolean searchReplace(String key, String value, List<String> lines)
//package com.java2s; //License from project: Apache License import java.util.List; public class Main { public static boolean searchReplace(String key, String value, List<String> lines) { return searchReplace(key, value, lines, false); }//w w w .j a v a 2 s .c o m public static boolean searchReplace(String key, String value, List<String> lines, boolean all) { boolean rs = false; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); if (cnfKeyOccursUnComment(key, line) || cnfKeyOccursCommented(key, line)) { lines.set(i, key + " = " + value); rs = true; if (!all) { break; } } } return rs; } public static boolean cnfKeyOccursUnComment(String key, String line) { return cnfKeyOccurs(key, line, 0); } public static boolean cnfKeyOccursCommented(String key, String line) { String ln = line.trim(); if (ln.length() == 0) { return false; } if (ln.charAt(0) == '#') { int idx = -1; for (int i = 1; i < ln.length(); i++) { if (ln.charAt(i) != ' ') { idx = i; break; } } if (idx > 0) { return cnfKeyOccurs(key, ln, idx); } } return false; } private static boolean cnfKeyOccurs(String key, String line, int startIdx) { String ln = line.trim(); if (ln.length() == 0) { return false; } if (ln.startsWith(key, startIdx)) { // next character must be whitespace or = // #abc.def = // i = 8 for (int i = startIdx + key.length(); i < ln.length(); i++) { char c = ln.charAt(i); if (c == ' ') { continue; } if (c == '=') { return true; } } } return false; } }