public static StringBuffer readContent(File file,Map<String[],String> replaceMap) throws IOException {
  StringBuffer content=new StringBuffer();
  BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file),ENCODING_UTF8));
  String line=null;
  String currentEndString=null;
  try {
    while ((line=reader.readLine()) != null) {
      if (currentEndString != null) {
        if (line.contains(currentEndString)) {
          currentEndString=null;
        }
        continue;
      }
      if (replaceMap != null) {
        for (        Entry<String[],String> entry : replaceMap.entrySet()) {
          if (line.contains(entry.getKey()[0])) {
            currentEndString=(entry.getKey()[1] != null && !line.contains(entry.getKey()[1])) ? entry.getKey()[1] : null;
            line=entry.getValue();
            break;
          }
        }
      }
      content.append(line + LINE_BREAK);
    }
  }
  finally {
    try {
      reader.close();
    }
 catch (    Exception ex) {
    }
  }
  return content;
}

public static StringBuffer readContent ( File file , Map < String [ ] , String > replaceMap ) throws IOException { StringBuffer content = new StringBuffer ( ) ; BufferedReader reader = new BufferedReader ( new InputStreamReader ( new FileInputStream ( file ) , ENCODING_UTF8 ) ) ; String line = null ; String currentEndString = null ; try { while ( ( line = reader . readLine ( ) ) != null ) { if ( currentEndString != null ) { if ( line . contains ( currentEndString ) ) { currentEndString = null ; } continue ; } if ( replaceMap != null ) { for ( Entry < String [ ] , String > entry : replaceMap . entrySet ( ) ) { if ( line . contains ( entry . getKey ( ) [ 0 ] ) ) { currentEndString = ( entry . getKey ( ) [ 1 ] != null && ! line . contains ( entry . getKey ( ) [ 1 ] ) ) ? entry . getKey ( ) [ 1 ] : null ; line = entry . getValue ( ) ; break ; } } } content . append ( line + LINE_BREAK ) ; } } finally { try { reader . close ( ) ; } catch ( Exception ex ) { } } return content ; }
reads the content of the file to a stringbuffer . each line is compared to the keys of the argument map . if a line is matched , the line is replaced with the keys corresponding value . passing null as replace map argument skips value replacement . the reading will stop at the first match for a single line .
