Io--randomaccessfile class

Source: Internet
Author: User

The Randomaccessfile class can implement random read and write operations on files.

    1. The file location pointer of the new Randomaccessfile object is located at the beginning of the file;
    2. After each read and write operation, the file position pointer will be shifted to read and write the number of bytes;
    3. The Getpointer () method is used to obtain the absolute position of the current file position pointer from the file header;
    4. Use the Seek () method to move the file position pointer (the Seek (long Pos) method moves the file position pointer to the absolute position specified by the parameter pos from the file header);
    5. The length () method returns the bytes of the file (depending on the length of the file and the position pointer, you can tell if the end of the file is read).
To insert a character in a file at the specified location:

public static void Main (string[] args) throws Exception {
Randomaccessfile RAF = new Randomaccessfile (New File ("Test.txt"), "RW");
Raf.seek (7);
StringBuffer sb = new StringBuffer ();
Byte[] B = new BYTE[20];
int Len;
/* Copy the contents of the pointer to SB */
while (len = Raf.read (b))! =-1) {
String str = new string (B,0,len);
Sb.append (str);
}
Raf.seek (7);//Move the pointer to the specified position
Raf.write ("xyz". GetBytes ());//write the character to be inserted
Raf.write (Sb.tostring (). GetBytes ());

Raf.close ();
}

Io--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.