在文件中间进行读写与
RandomAccessFile在Java中使用一样简单。
RandomAccessFile尽管有它的名字,但它更像是
InputStream和
OutputStream而不是像
File。它使您可以读取或查找
bytes文件,然后开始写您想停在的字节。
一旦发现此类,如果您对常规文件I / O有基本的了解,它将非常容易使用。
一个小例子:
public static void aMethod(){ RandomAccessFile f = new RandomAccessFile(new File("whereDidIPutTHatFile"), "rw"); long aPositionWhereIWantToGo = 99; f.seek(aPositionWhereIWantToGo); // this basically reads n bytes in the file f.write("Im in teh fil, writn bites".getBytes()); f.close();}


