Java base-io Stream object random Access file (randomaccessfile)
Yun Zhengjie
Copyright Notice: Original works, declined reprint! Otherwise, the legal liability will be investigated.
I. Randomaccessfile INTRODUCTION
Instances of this class support read and write to random-access files. Random access to a file behaves like a large byte array stored in the file system. There is a cursor or index that points to the suppressed array, called a file pointer , and the input operation reads the bytes from the file pointer and moves the file pointer forward as the byte is read.
Two. Randomaccessfile Usage Show
1 /*2 @author: Yinzhengjie3 Blog:http://www.cnblogs.com/yinzhengjie/tag/Java%E5%9F%BA%E7%A1%80/4 Email:[email protected]5 */6 7 PackageCn.org.yinzhengjie.note6;8 9 ImportJava.io.RandomAccessFile;Ten One /* A * - * Randomaccessfile (File file,string mode) - * Randomaccessfile (String name,string mode) the */ - - - Public classRandomaccessfiledemo { + Public Static voidMain (string[] args)throwsException { -Randomaccessfile RAF =NewRandomaccessfile ("Yinzhengjie.txt", "RW"); + //write Data of byte type ARaf.write (97); at //Print the current cursor position in the file - System.out.println (Raf.getfilepointer ()); - //write data of type int -Raf.writeint (200); - System.out.println (Raf.getfilepointer ()); - //data written to characters inRaf.writeutf ("Hello"); - System.out.println (Raf.getfilepointer ()); toRaf.writeutf ("World"); + System.out.println (Raf.getfilepointer ()); - the //positioning to the string offset, if it is a character, the position that needs to be moved is the length of the character. *Raf.seek (12); $ //read a stringPanax NotoginsengString s =Raf.readutf (); - System.out.println (s); the System.out.println (Raf.getfilepointer ()); + raf.close (); A } the } + - /* $ The result of the above code execution is as follows: $ 1 - 5 - A the + - WorldWuyi + the */
Java base-io Stream object random Access file (randomaccessfile)