Java uses the RandomAccessFile class to read and write file instance code based on pointers
The Java API provides a class that implements random access to files based on pointer operation. This class is the RandomAccessFile class, which is different from many other classes that read and write files based on stream. It inherits directly from object.
1. When using this class, you can specify the read-write mode of the file to be operated.
The first mode is read-only mode, and the second mode is read-write mode. Specified when creating an instance of this class.
2. Byte reading operation
(1) Void write (int d): write out the lower 8 bits of the int value and discard the other bits
(2) Int read(): Note: this method will only read a byte (8 bits) of data from the current pointer in the file, fill it into the eighth bit of the int return value, and fill the other bits with 0. If this method returns 0, it means that it is read to the end of the file.
Of course, using the above two methods will be very troublesome.
RandomAccessFile class also encapsulates the read and write operations of 8 basic data types, which will be very convenient to use.
Respectively
readByte(),readShort(),readInt(),readLong(),readFloat(),readDouble(),readBoolean(),readChar()
The return value types are the corresponding basic data types. At the same time, there are also writebyte () in these eight method. This is not available in streaming read-write files.
3. File pointer operation
All read and write operations of RandomAccessFile class are pointer based, and the pointer will move back with the read or write operations. At the same time, you can freely operate the position of the pointer through methods, so as to read and write files more conveniently.
common method
(1) Long getfilepointer(): this method is used to return the current pointer position. During the default read / write operation, the initial position of the pointer is the first byte of the file, that is, the value is 0 (2) void seek (long POS): this method can set the pointer position (3) int skipbytes (int n): this method can skip n bytes
summary
The above is all about Java using RandomAccessFile class to read and write file instance code based on pointers. I hope it will be helpful to you. Interested friends can continue to refer to this site: detailed explanation of static internal classes and code examples in Java, @ L_ 301_ 1 @ etc. if there are any deficiencies, please leave a message to point out. Thank you for your support!