在大型文本文件java中读写特定行

在大型文本文件java中读写特定行,java,bytebuffer,read-write,randomaccessfile,filechannel,Java,Bytebuffer,Read Write,Randomaccessfile,Filechannel,我使用Stream从文件中读取特定的行,在预先计算的字节位置值的帮助下,我使用FileChannel和ByteBuffer在一个大文件中写入相同的位置。基本上是编辑文本文件中的一些值 //读一行 Stream<String> lines = Files.lines(Paths.get('PATHTOFILE')); String text = lines.skip(LINENUMBER).findFirst().get(); //操作相同文本后,重新写入相同位置 RandomAcc

我使用Stream从文件中读取特定的行,在预先计算的字节位置值的帮助下,我使用FileChannel和ByteBuffer在一个大文件中写入相同的位置。基本上是编辑文本文件中的一些值

//读一行

Stream<String> lines = Files.lines(Paths.get('PATHTOFILE'));
String text = lines.skip(LINENUMBER).findFirst().get();
//操作相同文本后,重新写入相同位置

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();
我想知道有没有更干净或更好的方法,不用逐行阅读就能做到这一点


谢谢

如果存在“lines.skip(LINENUMBER).findFirst().get();”,则应该存在“lines.skip(LINENUMBER).findFirst().replace(“R”,R);“na”?它不会编辑文件中的数据。
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();