Random Read and Write file Randomaccessfile

Source: Internet
Author: User

The Randomaccessfile class is used for random access to a file, and itself has a method of reading and writing, through the skipbytes (int x), seek (int x) method to achieve the purpose of random access.
Instances of the Randomaccessfile 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 an implicit function group, called a file pointer.
This class is not a subclass of the IO system because it inherits directly from the object class, but it is a member of the IO package because it has read and write functionality. An array is encapsulated internally and manipulated by pointers to the elements of a group. You can get the pointer position through Getfilepointer, and you can change the position of the pointer by the Seek method.
In fact, the principle of complete reading and writing is to internally encapsulate the byte input stream and output stream.
With the Randomaccessfile constructor we can see that this class can only manipulate files and manipulate files and schemas.
The Randomaccessfile object's constructor will automatically create the file if it does not exist, and will not overwrite the existing file if it exists. If the mode is read-only, the file is not created, but it is read to an already existing file, if the file exists, an exception occurs, and if the mode is RW, the file is automatically created and, if present, the file is not overwritten.

Randomaccessfile Practical Application: The file is written in sections, a thread corresponding to a paragraph, is the principle of multi-threaded file download
Example:

 Public void WriteFile()throwsioexception{Randomaccessfile RAF =NewRandomaccessfile ("Raf.txt","RW"); Raf.write ("John Doe". GetBytes ()); Raf.writeint (255); Raf.close ();} Public void ReadFile()throwsioexception{Randomaccessfile RAF =NewRandomaccessfile ("Raf.txt","R");byte[] buf =New byte[4]; Raf.read (BUF);intnum = Raf.readint (); Raf.close ();} Public void WriteFile2()throwsioexception{Randomaccessfile RAF =NewRandomaccessfile ("Raf.txt","RW"); Raf.seek (8*3);//Adjust the pointer in the objectRaf.skipbytes (8);//Skips the specified number of bytes, but cannot jump backRaf.write ("Zhangsan". GetBytes ()); Raf.writeint ( -); Raf.close ();}

Random Read and Write file Randomaccessfile

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.