Java randomaccessfile Summary

Source: Internet
Author: User

I/O operations in javase are as follows:
Sometimes we need to append some content at the end of the file, so it is good to use randomaccessfile.
This class has two constructor methods:

RandomAccessFile(File file,

String mode)

Creates a random access file stream from which to read and write (optional) to it.
FileParameter.
RandomAccessFile(String name,

String mode)

Creates a random access file stream to read from and write to (Optional). The file has the specified name.

The mode parameter specifies the access mode used to open files. Allowed values and their meanings are:
"R" is opened in read-only mode. Any write method that calls the result object will throw an ioexception.
"RW" is enabled for reading and writing. If the file does not exist, try to create it.
"RWS" is enabled for reading and writing. For "RW", it is also required that each update of the file content or metadata be synchronized to the underlying storage device.
"RWD" is enabled for reading and writing. For "RW", you also need to synchronize every update to the file content to the underlying storage device.
The "RWS" and "RWD" modes work in an extremely similar way to the Force (Boolean) method of the filechannel class, passing the true and false parameters, unless they are always applied to each I/O operation, therefore, it is generally more efficient. If the file is located on a local storage device, when calling a method of this class is returned, you can ensure that all the changes made to this file by this call are written to the device. This is especially useful to ensure that important information is not lost during system crashes. This guarantee cannot be provided if the file is not on the local device.
The "RWD" mode can be used to reduce the number of I/O operations executed. To use "RWD", you only need to update the content of the file to be written to the storage. To use "RWS", you need to update the content and metadata of the file to be written, this usually requires at least one low-level I/O operation.

If the security manager exists, use the path name of the file parameter as its parameter to call its checkread method to check whether access to the file is allowed. If this mode allows writing, you can also use this path parameter to call the checkwrite method of the security manager to check whether write access to the file is allowed..

If you want to Append content to the end of the file, use the first constructor to call the randomaccessfile's seek (long LG) method. This method is passed into a long data type, we can just put file. length () is passed in, so that content is appended to the end of the file as required.

However, garbled characters may occur here. If you use the writebytes (string s) method of randomaccessfile to write content, garbled characters will occur, so it is best to use the write (byte [] B) method, example: String S = "Chinese"; RAF. write (S. getbytes (); this solves the garbled problem. Many people think that the two methods are similar. In fact, they are totally the opposite. The writebytes (string s) method splits Chinese characters into two bytes and writes them into the text, causing garbled characters, write (byte []
B) The method is to combine every two bytes into a Chinese character and write the text, so no garbled characters will appear.
We can also call the writeutf (string Str) method to avoid garbled characters.
There is also a problem to solve the line feed problem after writing "/N. In fact, "/R/N" is required.
"/R" means press enter, and return to the beginning of a line. "/N" refers to the line feed, and a line is added. Everyone knows this.
When we write a file in a program, the so-called "Carriage Return" should be the "carriage return ".
In Windows, the carriage return line character is "/R/N", but in Linux and other systems, there is no "/R" symbol. We generally use Windows, therefore, it should be "/R/N" to write files in a program.
Supplement: To meet cross-platform requirements, we recommend that you use system. getproperty ("line. separator") When you press enter to wrap ").

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.