Random Access file: Indicates that the data can be read anywhere in the file.
Public classrandomaccessfiledemo{ Public Static voidMain (string[] args)throwsException {File f=NewFile ("Raf.txt"); //write (f);read (f); } Private Static voidRead (File f)throwsException {//r = Read-onlyRandomaccessfile RAF =NewRandomaccessfile (F, "R"); System.out.println ("File pointer position:" + raf.getfilepointer ());//0 byteb =Raf.readbyte (); System.out.println (b);// -SYSTEM.OUT.PRINTLN ("File pointer position:" + raf.getfilepointer ());//1String str =Raf.readutf (); System.out.println (str);//DuncanSYSTEM.OUT.PRINTLN ("File pointer position:" + raf.getfilepointer ());//9Raf.seek (0);//Set file pointer locationSYSTEM.OUT.PRINTLN ("File pointer position:" + raf.getfilepointer ());//0Raf.skipbytes (9);//Set file pointer skips 9 bytesSYSTEM.OUT.PRINTLN ("File pointer position:" + raf.getfilepointer ());//9 inti =Raf.readint (); System.out.println (i); System.out.println ("File pointer position:" + raf.getfilepointer ());// -Raf.close (); } Private Static voidWrite (File f)throwsException {//RW means read and writeRandomaccessfile RAF =NewRandomaccessfile (F, "RW"); Raf.writebyte (65); Raf.writeutf ("Duncan");//using the modified UTF-8, two more bytesRaf.writeint (20); Raf.close (); }}
Randomaccessfile is often used to do multi-threaded downloads:
1. Multithreading
2. Multi-point Download
Java Core Class Library-io-random Access file (randomaccessfile)