The RandomAccessFile class reads and writes the file anywhere.
public class RandomAccessFile extends Object implements DataOutput, DataInput, Closeable
This class implements both the DataOutput and DataInput interfaces. Such instances support reading and writing random access files. Random access to files is similar to a large byte array stored in the file system. There is a cursor or index pointing to the hidden array, calledFile pointerThe input operation reads the byte starting from the file pointer and advances the object pointer as the byte is read. If a random access file is created in read/write mode, the output operation is also available. The output operation starts writing bytes from the file pointer and advances the file pointer as the bytes are written. The output operation after the current end of the hidden array causes the array to expand. The file pointer can be passed throughgetFilePointer()Method to read and passseek()Method settings.
Generally, if all reading routines in this class have reached the end of the file before reading the required number of bytesEOFException(YesIOException). If you cannot read any byte for some reason, instead of reaching the end of the file before reading the required number of bytesIOExceptionInsteadEOFException. It should be noted that if the stream has been closed, it may throwIOException.
Two constructor Methods
public RandomAccessFile(String name,String mode) throws FileNotFoundException
public RandomAccessFile(File file, String mode) throws FileNotFoundException
ModeThe parameter specifies the access mode used to open the file. Allowed values and their meanings are:
Value |
Meaning |
| "R" |
Open in read-only mode. AnyWriteMethods will throwIOException. |
| "Rw" |
Open for reading and writing. If the file does not exist, try to create it. |
| "Rws" |
Open for reading and writing,"Rw"Also, every update to the file content or metadata must be synchronized to the underlying storage device. |
| "Rwd" |
Open for reading and writing,"Rw"Also, every update to the file content is synchronized to the underlying storage device. |
"Rwd"Mode can be used to reduce the number of I/O operations executed. Use"Rwd"Only the content of the file to be written to the storage must be updated. Use"Rws"Update the content and metadata of the file to be written, which usually requires at least one low-level I/O operation.
Import java. io. IOException; import java. io. randomAccessFile; public class WriteTest {public static void main (String [] args) throws IOException {write (); read ();} private static void write () throws IOException {RandomAccessFile rf = new RandomAccessFile ("c: \ save.txt", "rw"); rf. writeByte ('1'); rf. writeChars ("android"); rf. writeUTF ("blog"); rf. close ();} private static void read () throws IOException {RandomAccessFile rf = new RandomAccessFile ("c: \ save.txt", "r "); // you can specify the read location rf. seek (2); System. out. println (char) rf. read (); rf. close ();}}
You can use the RandomAccessFile class to append files.
The random access in java I/O is not very clear.
Random file category: RandomAccessFile class
Features:
Supports "random access" to jump to any location for read/write.
The file indicator can be used to specify the position for reading and writing.
Practical application:
Resumable upload. Multi-threaded download.
Advantages:
It has a great advantage in random read/write and other long record format files.
Defects:
Only operation files are allowed, and other I/O devices cannot be accessed.
Rw // read/write r // read-only
What kind of group does RandomAccessFile use?
RandomAccessFile can access a file and perform read/write operations on any location of the file. The constructor has two parameters: one is a File-type instance or a string that represents the File address, and the other is a string that represents the access mode, such as "r" and "rw.