Summary of Java Randomaccessfile

Source: Internet
Author: User

Let's share with you today about I/O operations in Javase:
Sometimes we need to append some content to the end of the file, so it's good to use randomaccessfile at this point.
This class has two constructor methods:

randomaccessfile (file  file,string  mode)
           Creates a random-access file stream from which to read from and write to (optionally), the file is specified by the file parameter.
" Span style= "color: #ff0000;" >randomaccessfile (string  name, string  mode)
            creates a random-access file stream from which to read and write (optionally) a file with the specified name.

where the mode parameter specifies the access mode to open the file. The allowable values and their meanings are:
"R" "RW" open for read and write. If the file does not already exist, try to create the file.
"RWS" open for read and write, and for "RW", also requires that each update to the contents or metadata of the file be synchronously written to the underlying storage device.
"RWD"   open for read and write, and for "RW", each update to the file contents is also required to be written synchronously to the underlying storage device. The
RWS and RWD modes work much like The Force (Boolean) method of the FileChannel class, passing the true and false arguments, respectively, unless they are always applied to each I/O operation and are therefore generally more efficient. If the file is on a local storage device, when a call to a method of this class is returned, all changes made to the file by that call are guaranteed to be written to the device. This is especially useful for ensuring that important information is not lost when the system crashes. If the file is not on a local device, you cannot provide such a guarantee. The
RWD mode can be used to reduce the number of I/O operations performed. Using "RWD" only requires updating the contents of the file to be written to, and using "RWS" requires updating the content of the file to be written and its metadata, which typically requires at least one low-level I/O operation.

If a security manager is present, use the file The path name of the parameter calls its Checkread method as its argument to see if read access to the file is allowed. If the pattern allows writing, the Checkwrite method of the security manager is also invoked with the path parameter to see whether write access to the file is allowed for .

If you want to append content at the end of the file, use the first constructor, call Randomaccessfile's Seek (long LG) method, which passes in a long type of data, and we can pass the File.length () in, This appends the content to the end of the file as required.

But here may encounter garbled problem, if use of Randomaccessfile writebytes (String s) method write content, will appear garbled, so it is best to use write (byte[] b) method, for example: String s = "Chinese"; Raf.write (S.getbytes ()); This solves the garbled problem very well. Regarding these two methods, many people think that seems to be almost, in fact is the exact opposite way, writebytes (String s) method to split the Chinese into two bytes into the text, so it will cause garbled, write (byte[] b) method is to synthesize every two bytes a Chinese and write the text, So there is no garbled characters.
We can also call the writeUTF (String str) method to avoid garbled characters.
Knot down there is a solution to the problem of line, wrote "/n" no matter what the use AH. In fact, "/r/n" is the only line.
"/R" returns to the beginning of the line. "/n" is a newline and another line. That's what everyone knows.
When we write a file in the program, the so-called "carriage return" is exactly what it should be, "carriage return line break."
In Windows system, carriage return line symbol is "/r/n", but there is no "/r" symbol in Linux and other systems, we are generally under window, so that in the program to write a file is supposed to be "/r/n".
Add: In order to adapt to cross-platform requirements, we recommend that you use System.getproperty ("Line.separator") for line feeds.

Summary of Java Randomaccessfile

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: 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.