Randomaccessfile is a Java -provided access to the contents of a file, she can read a file, write a file, and randomaccessfile Supports random access to a file, which means he can specify a location to access it.
We know the Java file model, the file on the hard disk is a byte of byte byte to be stored, is a collection of data. Here are the steps to use this class.
(1) Open the specified file, there are two modes of " rw"( read/write ) "R"( Read-only ), create the object, and specify the file and schema, for example:
Randomaccessfile ac=new randomaccessfile (file, "RW");
Because it supports random access to files, he introduces pointers that can be written out in a specified position by a pointer. File pointer, opening the file when the pointer is at the beginning pointer=0
(2) Randomaccessfile method of writing to a file ( There are other ways to write )
Ac.write (int)-----> can write only one byte ( the last eight bits ), and thefile pointer moves, pointing to the next position.
(3) Randomaccessfile Read Methods ( There are other ways to read )
int b=ac.read ()---> read a byte
(4) after the file has been read and written, it must be shut down and called Close () the method. Here's an example:
//Create a file relative to the path, that is, create a folder in this projectFile file=NewFile ("Random"); if(!file.exists ()) {File.mkdir (); } File fileName=NewFile (file, "Javaio.txt")); if(!filename.exists ()) {filename.createnewfile ();//Create a file } //creates a Randomaccessfile object and specifies the pattern RW, which can read and write,//Note: Must be a file, cannot be a pathRandomaccessfile raf=NewRandomaccessfile (FileName, "RW"); //gets the position of the file pointer at this point where the starting position is 0System.out.println (Raf.getfilepointer ()); //writes the character A to the file, the character type has two bytes, but she writes the latter eight bits of bytes//the character a can be represented by a eight-bit byte.Raf.write (' A '); //the position of the character is automatically moved backwards, and the file pointer is automatically moved backwardsSystem.out.println ("Enter a character after the position of the file pointer" +Raf.getfilepointer ()); Raf.write (B); //Write only one byte per write, if I write it, I need to write it four times . intI=0x7fffffff; Raf.write (i>>>24 & 0xFF);//writes a high eight-bitRaf.write (i>>>16 & 0xFF); Raf.write (i>>>8 & 0xFF); Raf.write (i);//write low eight-bitSystem.out.println ("The position of the file pointer when writing an integer is" +Raf.getfilepointer ()); /*** Writeint () built-in method * Public final void writeint (int v) throws IOException {Write (v >>& Gt & 0xFF); Write ((v >>>) & 0xFF); Write ((v >>> 8) & 0xFF); Write ((v >>> 0) & 0xFF); Written + = 4; } */ //you can also write int integers directlyRaf.writeint (i); //write a Chinese character with a character of two bytesString str= "Welcome to Learning Java"; byte[] B=str.getbytes ("GBK"); Raf.write (b); System.out.println ("The length at this time is" +raf.length ()); //reading a file must place the file pointer at the beginningRaf.seek (0); byte[] buf=New byte[(int) Raf.length ()]; Raf.read (BUF);//writes content to the BUF byte arrayString str1=NewString (buf, "GBK"); System.out.println ("The content in the file is" +str1); Raf.close ();
Java Randomaccessfile reads and writes the contents of a file