A Brief Introduction to the RandomAccessFile class and code demonstration

Source: Internet
Author: User

RandomAccessFile class, supports reading and writing files. You can also set the write and read locations. This location is mainly represented in bytes. This file is stored in a large byte [] array. There is a pointer to the array. Similar to this type of object, you can set this implicit pointer position to read and write files from any position. The construction method of the RandomAccessFile class. RandomAccessFile (String name, String mode); // name is the object name associated with this type of object. Mode is the access mode for opening files. RandomAccessFile (File file, String mode); // file is the associated File. Mode. Mode has four values: Value Meaning "r" is opened in read-only mode. Any write method that calls the result object will throw an IOException. "Rw" is enabled for reading and writing. If the file does not exist, try to create it. "Rws" is enabled for reading and writing. For "rw", it is also required that each update of the file content or metadata be synchronized to the underlying storage device. "Rwd" is enabled for reading and writing. For "rw", you also need to synchronize every update to the file content to the underlying storage device. Main method: void write (byte [] B); write B. length bytes to the specified position of the file. Void write (int B); // write the specified byte to the file. This method only writes the lowest eight bits. For example, write (97) and write (609) write the same content. All are 97 letters. Void writeInt (int B); // write the int to the file in four bytes. Write high first. Int read (); // read a Data byte from the file. Int read (byte [] B); // read up to B. length data bytes from the file to the modified byte array. Int readInt (); // read a signed 32-bit integer from the file. Void seek (long pos); // sets the implicit pointer position offset for file read/write. Long getFilePointer (); returns the current offset of the object. Void close (); // close this random access file stream and release all system resources associated with the stream. Code Demonstration: [java] package RandromAccessDemo; import java. io. IOException; import java. io. randomAccessFile; public class RandomAccessDemo {/*** @ param args * @ throws IOException */public static void main (String [] args) throws IOException {// write (); // read (); randomWriter ();} public static void randomWriter () throws IOException {/** can be written to the same file in multiple threads. */RandomAccessFile raf = new RandomAccessFile ("Random.txt", "rw"); // raf. seek (3*8); // set the pointer position raf. write ("Wu Song ". getBytes (); raf. writeInt (103);} public static void read () throws IOException {RandomAccessFile raf = new RandomAccessFile ("Random.txt", "r"); raf. seek (1*8); byte [] buf = new byte [4]; raf. read (buf); String name = new String (buf); int age = raf. readInt (); System. out. println ("name:" + name +" Ge: "+ age); System. out. println (" getFilePointer: "+ raf. getFilePointer (); // obtain the current pointer position. Raf. close ();} public static void write () throws IOException {/** when a file exists, it is not created and written from the specified position. If the file does not exist, the file is created. */RandomAccessFile raf = new RandomAccessFile ("Random.txt", "rw"); raf. write ("wangcai ". getBytes (); // raf. write (97); // a write only writes at least eight bits // raf. writes (609); // a raf. writeInt (97); raf. write ("Li Yun ". getBytes (); raf. writeInt (99); raf. close ();}}

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.