bytebuffer - Read and write to a particular line in large textfile java -
i'm using stream read particular line file , of pre-calculated byte position value i'm using filechannel , bytebuffer write same location in big file. editing values in textfile.
//read particular line
stream<string> lines = files.lines(paths.get('pathtofile')); string text = lines.skip(linenumber).findfirst().get();
// modify text example
text.replace("r",r);
// re-write same position after manipulating same text
randomaccessfile file = new randomaccessfile('pathtofile', "rw"); filechannel channel= file.getchannel(); bytebuffer buf = bytebuffer.allocate(50); buf.put(text) buf.flip() channel.position(pre_calculated_position); while(buf.hasremaining) channel.write(buf) channel.close(); buf.clear(); file.close();
i'm wondering if there cleaner or better way same without reading line line?
thanks
Comments
Post a Comment