The--randomaccessfile of Java IO operations

Source: Internet
Author: User

Goal:

1) Mastering the role of the Randomaccessfile class.

2) Read the data at the specified location with Randomaccessfile.

Specific content

The main function of the Randomaccessfile class is to complete the random read function, which can read the contents of the specified range.

Suppose the following three data is stored in the file:

Because everything in the file is stored in bytes, there is a fixed save location.

Construction Method:

 Public Randomaccessfile (file filethrows FileNotFoundException

Instantiating this class requires passing the file class , telling the program which files should be manipulated. Then there is a pattern, the open mode of the file,

Two commonly used modes:

1) r: Read mode

2) W: Write mode

3)RW: Read-write mode, if this mode is used, if the file does not exist, it will be created automatically .

 PackageThread1;ImportJava.io.File;ImportJava.io.RandomAccessFile; Public classdemo1{//all exceptions are thrown directly and are no longer processed in the program     Public Static voidMain (String args[]) throws    Exception{ //using throws at the main method throws an exception to the JVM for processing,
Save a lot of exception handling operations
File F =NewFile ("D:" +File.separator+ "test.txt");//specify the files to manipulateRandomaccessfile RDF =NULL;//declaring an object of the Randomaccessfile class RDF = new Randomaccessfile (f, "RW");//read-write mode, if the file does not exist, it is automatically createdString name =NULL ; intAge = 0 ; Name= "Zhangsan";//string length is 8Age = 30;//the length of the number is 4 rdf.writebytes (name); Write name to file Rdf.writeint (age); Write age to FileName = "Lisi";//string length is 8age = 31;//the length of the number is 4 rdf.writebytes (name); Write name to File Rdf.writeint (age); Write age to FileName = "Wangwu";//string length is 8Age = 32;//the length of the number is 4 rdf.writebytes (name); Write name to File Rdf.writeint (age); Write age into File rdf.close (); Close }};

 method Written: writebytes (String str); writeint (int a);

Operation Result:

The above completes the write operation. Each piece of data is 12 bits.

Start reading data after writing. When writing, it is possible to write a string (writebytes (String str)) and read it in bytes (ReadByte ()).

skip a few bytes of method: Skipbytes (n), a few bytes forward method: Seek (0), array and string conversion method: String Str=new string (char b[]).

ImportJava.io.File;ImportJava.io.RandomAccessFile; Public classrandomaccessfiledemo02{//all exceptions are thrown directly and are no longer processed in the program     Public Static voidMain (String args[])throwsexception{File F=NewFile ("D:" + File.separator + "test.txt");//specify the files to manipulateRandomaccessfile RDF =NULL;//declaring an object of the Randomaccessfile classRDF =NewRandomaccessfile (F,"R");//Open file in read-only modeString name =NULL ; intAge = 0 ; byte b[] = new BYTE[8]; Open a byte array//Read the second person's information, meaning to empty the first person's information       Rdf.skipbytes (12); Skip the first person's message         for(inti=0;i<b.length;i++) {B[i]= Rdf.readbyte ();//read one byte} name=New String (b); Converts the read byte array into a string, where the array is placed into a string object.       Age = Rdf.readint (); Reading the number//readint () reads exactly 4 bytes, reading a few digits following the preceding 8 strings. System.out.println ("The second person's information--Name:" + name + "; Age:" +Age ); //read the first person's information        rdf.seek (0); Pointer back to the beginning of the file         for(inti=0;i<b.length;i++) {B[i]=  rdf.readbyte ();  // read one byte   } name=NewString (b);//converts a read byte array into a stringAge = Rdf.readint ();//reading numbersSystem.out.println ("First person's information--Name:" + name + "; Age:" +Age ); Rdf.skipbytes (12);//empty The information for the second person         for(inti=0;i<b.length;i++) {B[i]= Rdf.readbyte ();//read one byte} name=NewString (b);//converts a read byte array into a stringAge = Rdf.readint ();//reading numbersSystem.out.println ("The third person's information--Name:" + name + "; Age:" +Age );                Rdf.close (); //Close    }};

Operation Result:

The second person's information--name: Lisi    ; age:--and name: Zhangsan;Age: 32-and name: Wangwu  ; Age:

If you want to complete the file content operation, you can use Roundomaccessfile () to complete.

The--randomaccessfile of Java IO operations

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.