Random Read and write Randomaccessfile

Source: Internet
Author: User

The Randomaccessfile class is a Java class that can read and write randomly, and can dynamically move pointers for Random writes

Constructor information:

Main method: If you write on the position where the Seek method is set, the content on the current year's location will be overwritten

Package Cn.bean.demo.random;import Java.io.ioexception;import Java.io.randomaccessfile;public class randomaccessfiletest {public static void main (string[] args) {testrandomreadwrite ();} static void Testrandomreadwrite () {//Create a random read-write instance [RW stands for Read and write, R for read-only, cannot write]try (randomaccessfile accessfile=new Randomaccessfile ("String1.txt", "RW");) {//Insert a Hello china//in the second line of the file to read the first line of content and move the pointer to the end of the first line string line= Accessfile.readline ();//Read the rest of the content into a buffer [if the contents of the file is too large for memory consumption, the remaining content should be written to a disk file, so it is best to determine the size of the file when randomly inserted, in processing]byte[] Buff=new byte[(int) (Accessfile.length ()-line.length ())];//reads the remaining content into the buff accessfile.read (buff);//After reading the pointer at the end of the file, Move the pointer position before inserting Accessfile.seek (Line.length ());//Insert Hello Chinaaccessfile.writebytes ("\nhello china\n");} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

  

Random Read and write 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.