Java 21-12 random Access stream (not part of IO stream)

Source: Internet
Author: User

Random Access stream:
  The Randomaccessfile class does not belong to a stream and is a subclass of the object class.
But it incorporates the functions of InputStream and OutputStream.
  Support for random access to files read and write.

Public Randomaccessfile (String name,string mode): The first parameter is the file path, the second parameter is the mode of the action file

mode: parameter specifies the access mode used to open the file. The allowed values and their meanings are:

Value

Implications

"R" Open as read-only. Any write method that invokes the resulting object will cause the throw IOException .
"RW" Open for Read and write. If the file does not already exist, try to create the file.
"RWS" Open for Read and write, and for "RW", each update to the file's contents or metadata is also required to be written to the underlying storage device synchronously.
"RWD" Open for Read and write, and for "RW", each update to the file content is also required to be written synchronously to the underlying storage device.

There are four modes, one of our most commonly used is called "RW", which means that both the data can be written and the data can be read

First, we create a random access stream and write the data

1     Private Static voidWrite ()throwsIOException {2         //Create a random-access stream object3        Randomaccessfile RAF = new randomaccessfile ("Raf.txt", "RW"); 4 5         //can write to any type of data6Raf.writeint (100);7Raf.writechar (' a ');8RAF.WRITEUTF ("China"));9 Ten raf.close (); One}

Try reading the random access stream again:

1 Private Static voidRead ()throwsIOException {2         //Create a random-access stream object3        Randomaccessfile RAF = new randomaccessfile ("Raf.txt", "RW");//Read or write, this code is the same 4 5         inti =raf.readint ();6 System.out.println (i);7         //The file pointer can be read by the Getfilepointer method and set through the Seek method. 8SYSTEM.OUT.PRINTLN ("The pointer position of the current file is:" + raf.getfilepointer ());//49 Ten         CharCH =Raf.readchar (); One System.out.println (CH); ASYSTEM.OUT.PRINTLN ("The pointer position of the current file is:" + raf.getfilepointer ());//6 -  -String s =Raf.readutf (); the System.out.println (s); -SYSTEM.OUT.PRINTLN ("The pointer position of the current file is:" + raf.getfilepointer ());//16, after 6+3+3, also read 2 bytes +2 = -  -         //If you just read a: +Raf.seek (4); -ch = Raf.readchar ();//To understand the location of the data to be read what type, here to correspond to the type, otherwise you will find that the output of things can not understand + System.out.println (CH); A}

Java 21-12 random Access stream (not part of IO stream)

Related Article

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.