[Java iO] _ randomaccessfile notes
Objectives of this chapter:
Measure the test taker's knowledge about the role of the randomaccessfile class.
You can use randomaccessfile to read data at a specified location.
The main function of the randomaccessfile class is to complete the random reading function, which can read content at a specified location.
Randomaccessfile reads data in bytes. For details about the length of data types in bytes, refer to the following:
How many bytes of a character in Java?
Http://blog.csdn.net/e421083458/article/details/8518578
Number of characters occupied by strings in different character encodings in Java
Http://blog.csdn.net/e421083458/article/details/8518601
Public randomaccessfile (File file, string mode) throws filenotfoundexception
Mode: Enable mode:
R: Read mode
W: Write only
RW: read/write. If this mode is used, it is automatically created if the file does not exist.
Import Java. io. file; import Java. io. randomaccessfile; public class randomaccessfiledemo01 {// All exceptions are thrown directly. Public static void main (string ARGs []) is not processed in the program. throws exception {file F = new file ("D:" + file. separator + "test.txt"); // specify the file randomaccessfile RDF = NULL; // declare the object of the randomaccessfile class (RDF = new randomaccessfile (F, "RW "); // read/write mode. If the file does not exist, string name = NULL is automatically created. Int age = 0; name = "zhangsan"; // the string length is 8 age = 30; // The number length is 4 RDF. writebytes (name); // write the name to the file in RDF. writeint (AGE); // write age into the file name = "Lisi"; // the string length is 8 age = 31; // The number length is 4 RDF. writebytes (name); // write the name to the file in RDF. writeint (AGE); // write age into the file name = "wangwu"; // the string length is 8 age = 32; // The number length is 4 RDF. writebytes (name); // write the name to the file in RDF. writeint (AGE); // write the age into the file in RDF. close (); // close }};
Import Java. io. file; import Java. io. randomaccessfile; public class randomaccessfiledemo02 {// All exceptions are thrown directly. Public static void main (string ARGs []) is not processed in the program. throws exception {file F = new file ("D:" + file. separator + "test.txt"); // specify the file randomaccessfile RDF = NULL; // declare the object of the randomaccessfile class (RDF = new randomaccessfile (F, "R "); // open the file in read-only mode: string name = NULL; int age = 0; byte B [] = new byte [8]; // open a byte array // read the information of the second person, which means that the information of the first person should be blank. skipbytes (12); // skip the information of the first person for (INT I = 0; I <B. length; I ++) {B [I] = RDF. readbyte (); // read a byte} name = new string (B); // convert the read byte array to the string age = RDF. readint (); // read the number system. out. println ("second person's information --> name:" + name + "; age:" + age); // reads the information of the first person in RDF. seek (0); // the pointer returns to the beginning of the file for (INT I = 0; I <B. length; I ++) {B [I] = RDF. readbyte (); // read a byte} name = new string (B); // convert the read byte array to the string age = RDF. readint (); // read the number system. out. println ("First Person's information --> name:" + name + "; age:" + age); RDF. skipbytes (12); // empty the information of the second person for (INT I = 0; I <B. length; I ++) {B [I] = RDF. readbyte (); // read a byte} name = new string (B); // convert the read byte array to the string age = RDF. readint (); // read the number system. out. println ("Third Person's information --> name:" + name + "; age:" + age); RDF. close (); // close }};
Summary:
Byte B [] = new byte [8]; // open a byte array
For (INT I = 0; I <B. length; I ++ ){
B [I] = RDF. readbyte (); // read one byte, because only one byte can be read at a time.
}
Name = new string (B); // convert the read byte array to a string