First use of the Randomaccessfile class

Source: Internet
Author: User

Randomaccessfile:

Java provides access to file content that can read files or write files

Support random access files anywhere you can access files

(1) Java File Model:

A file on the hard disk is a byte byte byte that is a collection of data stored

(2) Open file

There are two modes of RW read-write R read-only

Randomaccessfile raf=new randomeaccessfile (file, "RW");

file pointer, pointer=0;

(3) How to write

Raf.write (int)----> write only one byte (last 8 bits) while the pointer points to the next position ready to write again

(4) Method of Reading

int B=raf.read (); Read a Byte

(5) The file must be closed after reading and writing (Oracle official note)

Raf.getfilepointer () Gets the current pointer position

Raf.length () Get file length

Raf.seek () Assigning a pointer to a location




1 ImportJava.io.File;2 Importjava.io.IOException;3 ImportJava.io.RandomAccessFile;4 Importjava.util.Arrays;5 6 7 8 9 Public classRafdemo {Ten One /** A * @paramargs - */ - Public Static voidMain (string[] args)throwsioexception{ theFile demo=NewFile ("Demo"); - if(!demo.exists ()) - Demo.mkdir (); -File file=NewFile (demo, "Raf.dat")); + if(!file.exists ()) - { + file.createnewfile (); A } at -Randomaccessfile raf=NewRandomaccessfile (file, "RW"); - //position of the pointer - System.out.println (Raf.getfilepointer ()); - -Raf.write (' A ');//only one byte was written in System.out.println (Raf.getfilepointer ()); -Raf.write (' B '); to + intI=0x7fffffff; - //Write only one byte at a time with write, 4 times the //0xff=00000000 00000000 00000000 11111111 * //and Byte (8bit) is actually given to int (4byte) after the first 24 bits of the complement 0, obtained is the last 8 bits. $Raf.write (i>>>24);//8-bit highPanax NotoginsengRaf.write (i>>>16); -Raf.write (i>>>8); the Raf.write (i); + System.out.println (Raf.getfilepointer ()); A the //can write directly to an int + Raf.writeint (i); - System.out.println (Raf.getfilepointer ()); $ $String s= "Medium"; - byte[] Gbk=s.getbytes ("GBK"); - Raf.write (GBK); the System.out.println (Raf.getfilepointer ()); - Wuyi //read the file and move the pointer over the head theRaf.seek (0); - //One-time read to read the contents of a file into a byte array Wu byte[] buf=New byte[(int) Raf.length ()]; - Raf.read (BUF); About System.out.println (arrays.tostring (BUF)); $ -String s1=NewString (buf, "GBK"); - //System.out.println (S1); - for(byteb:buf) { ASystem.out.print (integer.tohexstring (b& 0xff) + ""); + } the raf.close (); - } $ the}

First use of the Randomaccessfile class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.