Java IO learning notes (2): RandomAccessFile class

Source: Internet
Author: User

The previous File class only operates on the File itself. If you operate on the File content, you can use the RandomAccessFile class, you can randomly read data at a specified position in a file.

Because all the content in the file is stored in byte and has a fixed storage location.

 

Constructor:

Public RandomAccessFile (File file, String mode) throws FileNotFoundException

When instantiating this class, you must pass the File class. Tell the program which file should be operated, followed by a mode, file opening mode, two common modes:

  • R: Read
  • W: Write
  • Rw: read/write. If this mode is used, if the file does not exist, it is automatically created.

Write information first:

1 import java. io. File;
2 import java. io. IOException;
3 import java. io. RandomAccessFile;
4
5 public class Test9 {
6 public static void main (String [] args) throws IOException {
7 File f = new File ("d:" + File. separator + "test.txt ");
8 RandomAccessFile raf = new RandomAccessFile (f, "rw"); // read/write mode. If the path does not exist, it is automatically created.
9 String name1 = "jim ";
10 int age1 = 20;
11 String name2 = "Tom ";
12 int age2 = 30;
13 raf. writeBytes (name1 );
14 raf. writeInt (age1 );
15 raf. writeBytes (name2 );
16 raf. writeInt (age2 );
17 raf. close ();
18}
19}

Then read the file:

1 import java. io. File;
2 import java. io. IOException;
3 import java. io. RandomAccessFile;
4
5 public class Test10 {
6 public static void main (String [] args) throws IOException {
7 File f = new File ("d:" + File. separator + "test.txt ");
8 RandomAccessFile raf = new RandomAccessFile (f, "r"); // open in Read mode
9 raf. skipBytes (7); // skip the information of the first person
10 byte [] bs = new byte [3];
11 for (int I = 0; I <bs. length; I ++ ){
12 bs [I] = raf. readByte ();
13}
14 String name2 = new String (bs );
15 int age2 = raf. readInt ();
16 System. out. println (name2 + "" + age2 );
17
18 raf. seek (0); // the pointer returns to the beginning of the file and reads the information of the second person.
19 for (int I = 0; I <bs. length; I ++ ){
20 bs [I] = raf. readByte ();
21}
22 String name1 = new String (bs );
23 int age1 = raf. readInt ();
24 System. out. println (name1 + "" + age1 );
25}
26}

In addition: Some students may enter Chinese, then the use of getBytes () conversion, will occupy 3 bytes (depending on your local encoding, if you can set to Eclipse as a UTF-8), bytes? Does Chinese in java occupy two bytes? How can it be changed to 3 bytes? In fact, this is not a concept. java's default character encoding is unicode, which actually occupies two bytes. However, the getBytes () encoding used for conversion from String to byte [] is used by default, so it will occupy 3 bytes, unicode and UTF-8 is not a concept, if you carefully observe it will find that the getBytes () method has another heavy load getBytes ("...... "). For example, if getBytes (" gbk ") is used, the converted Chinese character will be changed to two bytes.

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.