Replace write() with add() for StringBuffer