Hi All,
I want to search a particular text ina file and replace it with a different string (much more in length).
I am tryinh it with regular expression and new io.
here is my code snippet
.....................................
RandomAccessFile raf=new RandomAccessFile(f,"rw");
FileChannel fc = raf.getChannel();
int len=(int)fc.size();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_WRITE, 0, len);
Charset charset = Charset.forName("US-ASCII");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer cb = decoder.decode(bb);
Pattern p = Pattern.compile("texttomatch");
Matcher m = p.matcher(cb); // get a matcher object
StringBuffer sb = new StringBuffer();
byte[] src=null;
while (m.find()) {
m.appendReplacement(sb, "texttoreplace with");
///src=new String(cb.array()).getBytes();
///writeBuffer.put(src);
//bb.put(sb.toString().getBytes());
}
m.appendTail(sb);
bb.force();
//raf.close();
fc.close();
.....................................
When i see the string buffer the text is correctly replace but i am not able to put it in mapped buffer.
If anybody has some solution or some better way ,then please let me know