Java------radnomaccessfile

Source: Internet
Author: User
Tags readfile

Randomaccessfile:

Random access to the file, its own method of reading and writing. by skipbytes (int x), seek (int x) to achieve random access.

This class is not a sub-class of IO system, but directly inherits from object

However, it is a member of the IO package because it has read and write capabilities.

random access to a file behaves like a large byte array stored in the file system. There is a cursor or index pointing to the suppressed array, calledFile PointersThe input operation reads the bytes from the file pointer and moves the file pointer forward as it reads the bytes. if the random Access file is created in read/write mode, the output operation is also available, and the output operation starts writing bytes from the file pointer and moves the file pointer forward as it writes to the byte. Writing an output operation after the current end of the suppressed array causes the array to expand. The file pointer can beGetfilepointermethod is read and passedSeekmethod Settings. In general, if all the read routines in this class have reached the end of the file before reading the required number of bytes, throweofexception(is aIOException). If, for some reason, no bytes can be read, instead of having reached the end of the file before reading the required number of bytes, throwIOException, but noteofexception. It should be noted that if the stream is closed, it may throwIOException.

Construction Method Summary

Randomaccessfile (file file, String mode)
creates a random-access file stream from which to read from and write to (optional), the file is created by the The File parameter is specified.

Randomaccessfile (string name, string mode)
Creates a random-access file stream from which to read and write (optionally) a file with the specified name.

Mode: Operating modes

"R" opens as read-only. Any write method that invokes the result object will cause the IOException to be thrown.

" RW "Open for Read and write.  If the file does not already exist, try to create the file.

" RWS "Open for Read and write, for" RW  ", it also requires that each update to the contents or metadata of the file be synchronously written to the underlying storage device.

" RWD "Open for Read and write, for" RW  ", it also requires that each update to the file's contents be synchronously written to the underlying storage device.


If the mode is R (read-only), the file is not created and an exception occurs if the file does not exist

If the mode is RW, the file does not exist and is automatically created, if present, does not overwrite


Randomaccessfile supports reading and writing files from any location, which is very consistent with the principle of multi-threaded downloads.

Assuming a file size of 20, then open 4 threads, each thread download 5

0--5

6--10

11-15

16--20

To create a Randomaccessfile object:

Randomaccessfile RAF = new Randomaccessfile ("Data.txt", "RW");


Read and Write methods:

Raf.write ("Zhang San". GetBytes ()); Raf.writeint (); byte buff[] = new Byte[4];raf.read (buff);


Adjusting pointers in objects

Raf.seek (8);


Skips a specified number of bytes

Raf.skipbytes (8);


Give me a chestnut:

public class Randomaccessfiledemo {public static void main (string[] args) throws Exception {WriteFile ();////readFile (); /WriteFile2 ();//start writing files from the specified position writeFile3 ();//start writing files from the middle of the original file, overwriting the equivalent length content after the current pointer}private static void WriteFile3 () throws Exception {Randomaccessfile RAF = new Randomaccessfile ("Data.txt", "RW");//Seek and skipbytes two methods will overwrite the content after//Raf.seek (8); Raf.skipbytes (8); Raf.write ("Lao Li". getBytes ()); Raf.write (102); Raf.close ();} public static void WriteFile2 () throws Exception {Randomaccessfile RAF = new Randomaccessfile ("Data.txt", "RW");//from 24 bytes After starting to write Raf.seek (8 * 3); Raf.write ("Period". GetBytes ()); Raf.write (103);} public static void ReadFile () throws Exception {Randomaccessfile RAF = new Randomaccessfile ("Data.txt", "R");//Raf.write ( "Hahaha". GetBytes ());//Adjust the object in the pointer//Raf.seek (8);//Read the file from the 8th byte, read the Reese//skip the specified number of bytes//raf.skipbytes (8); byte buff[] = new Byte[4];raf.read (Buff); string s = new string (buff); System.out.println ("name =" + s); int age = Raf.readint (); System.out.println ("age =" + age); RAF.CLose ();} public static void WriteFile () throws Exception {Randomaccessfile RAF = new Randomaccessfile ("Data.txt", "RW"); Raf.write ( "Zhang San". GetBytes ()); Raf.writeint ("Reese") Raf.write (GetBytes ()); Raf.writeint (); Raf.close ();}}


Java------radnomaccessfile

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.