First, brief
This is on the JDK, and we can see that its parent class is object, with no inherited byte stream or any class in the character stream family. And it implements the two interfaces of Datainput and dataoutput, which means that the class can be read and written.
Second, the significance of existence
1. Is the most feature-rich file content access class in Java I/O streaming system, it provides many methods to access the file content.
2, because you can freely access any location of the file, so if you need to access some of the contents of the file, Randomaccessfile will be a better choice.
3, can be used to access the file to save data records, the size of the file should not be the same, but its size and location must be known.
This class in many materials translated into Chinese are: random access to files, in Chinese, random is an indeterminate meaning, refers to a visit here, a visit to the meaning of there. If it is explained by this semantics, it will be confusing. In fact, the random in English is not only stochastic, but also arbitrary meaning. If the Chinese name is arbitrary access to the file is not a better understanding. Arbitrary means that we can specify any location in the file to manipulate a file.
Three, demo demo (1), write file
1 public class Randomaccessfiletest 2 {3 public static void Main (string[] args) throws IOException 4 {5 Rand Omaccessfile RAF = new Randomaccessfile ("D:/data.txt", "RW"); 6 person p = new Person (1001, "Xiaoming", 1.80d); 7 P.write (RAF); 8 } 9}10 class Person11 { int id;13
string name;14 Double height;15 Public Person () + {}18 public person (int id, String name, Double height) { this.id = id;21 this.name = name;22 this.height = height;23 }24 25 Public Void Write (Randomaccessfile RAF) throws IOException26 { raf.write (ID); raf.writeutf (name); raf.writedouble (height); 30
Execution Result:
1, the execution results garbled is due to the binary file is written, this will later we use the program to read. (This is similar to the DataInputStream and DataOutputStream described earlier)
2. In line five, Randomaccessfile's constructor specifies the file to be written, and another parameter: MoD, which is used primarily to specify the access mode of the open file.
3, read the way is to read the basic data type, where the 28th line uses the method is:writeUTF(String str)
using modified UTF-8 encoding to write a string to the file in a machine-independent way, this method is to write the string to the file without worrying about garbled, because the encoding used is UTF-8
(2), file read
Since the binary file was just written, the program is now used to read the file:
1 public class Randomaccessfiletest 2 {3 public static void main (string[] args) throws IOException 4 {5 Randomaccessfile RAF = new Randomaccessfile ("D:/data.txt", "RW"); 6 person p = new person (1001, "Xiaoming", 1.80d); 7 P.write (RAF);//After writing the file, the pointer to any access file at the end of the file 8 9 raf.seek (0);//reads, resets the pointer to the beginning of the file. P2 = new Person (), P2.read (RAF), System.out.println ("id=" + p2.getid () + "; Name=" + P2.getname () + "; height=" + p2.getheight ());}16}17 class Person18 {int id;20 strin G name;21 double height;22 Public person () @}25 public person (int ID, String name, double height) This.id = id;28 THIS.name = name;29 this.height = height;30}31 Public v OID write (Randomaccessfile RAF) throws IOException33 {raf.writeint (ID); Raf.writeutf (name); 36 raf.writedouble (height); 37}38 39 public void Read (Randomaccessfile RAF) throws IOException40 {this.id = Raf.readint (); this.name = Raf.readutf (); this.height = Raf.readdouble ();}45 public int getId () 8}49 public void setId (int id)-{this.id = id;52}53 public String getName () 54 {55 return name;56}57 public void SetName (String name), this.name = name;60}61 Public Double GetHeight (), {height;64}65 public void setheight (double height) 66 {67 This.height = height;68}69 70}
Execution Result:
id=1001;name=xiaoming;height=1.8
1, in 39-43 lines of code, because it is written and read by the basic data type, so at the time of reading must strictly follow the order of writing.
2, the position of the 9th line, because at the time of writing, the position of the pointer that leads to access at the end of the file, now read, you need to reset the location of the access pointer to the beginning of the file.
(3), additional content
1 public static void main (string[] args) throws IOException2 {3 randomaccessfile RAF = new Randomaccessfile ("d:/o Ut.txt "," RW "), 4 Raf.seek (Raf.length ()); 5 raf.write (" \ r \ n China Mobile reading base ". GetBytes ()); 6 }
Execution Result:
1. This program demonstrates the contents of the original content of the document to append content. Where the Seek method is to move the access pointer to the end of the file content.
2, Randomaccessfile still can only append, can not be like the file in the specified location to insert content. If you force the file record pointer to move to an intermediate position and start outputting content, the new content overwrites the contents of the file.
3, if you need to insert content to the location specified in the file, the program needs to read the contents after the insertion point into the buffer, and so on after the insertion is complete, then the contents of the buffer is appended to the file.
(4), specify position insertion
1 public static void main (string[] args) 2 {3 Try 4 {5 Insert ("D:/out.txt", 5, "inserted content "); 6} 7 catch (IOException E) 8 {9 e.printstacktrace (); 10}11}12 13 private static void Insert (String filename,long pos,string content) throws IOException14 {15//Create temporary empty file 16 File Tempfile = file.createtempfile ("temp", null); 17//When the virtual machine terminates, request to delete files or directories represented by this abstract path name. tempfile.deleteone XIT (); FileOutputStream fos = new FileOutputStream (tempfile); Randomaccessfile RAF = new Ra Ndomaccessfile (FileName, "RW"); Raf.seek (POS); byte[] buffer = new byte[4];24 int num = 0;25 while ( -1! = (num = raf.read (buffer))): Fos.write (buffer,0,num);}29 Raf.s EEK (POS); Raf.write (Content.getbytes ()); FileInputStream fis = new FileInputStream (tempfile); 32 while ( -1! = (num = Fis.read (buffer)) {raf.write (buffer,0,num); 35}36}
Execution Result:
1, the principle of inserting the content here is: the insertion point after the content read into the buffer, and so on after the insertion is complete, then the contents of the buffer is appended to the file after.
Deep understanding of Java I/O Series IV: Randomaccessfile