randomaccessfile★ random access to the file, its own method of reading and writing.
After new Randomaccessfile (), if the file does not exist, it is created automatically, and exists. -This class actually encapsulates both the byte input stream and the byte output stream.
If the class writes an integer using the Write () method, it writes only its last byte at a time. With the Writeint () method, an integer can be written in full.
★ by Skipbytes (int x), seek (int x) to achieve random access.
You can use the Seek method to set a pointer to the data to achieve random read and write to the file data. The Skip () method in InputStream can only jump from the back, not reverse, and the Seek () method can be positioned in both directions.
★ Features of data modification
With the Randomaccessfile class can be used to achieve data modification, of course, the data in the file should be regular, in order to facilitate the programming can be positioned, so that the data written to the place.
When using "flow" to implement data modification, it is usually necessary to read the data from the stream into the array, modify the data in the array, and then re-write the modified array to the stream.
Serialization ★ Serialization Introduction
Storing an object on some type of persistent memory is called holding. If an object can be stored on a disk or tape, or can be sent to another machine and placed on a memory or disk, then this object is known to be persisted. (in Java, serializing, persisting, serialization is a concept.) )
The Java.io.Serializable interface does not have any method, it is only as a "reporter", to indicate that the class that implements this interface can consider serialization. Objects in a class that do not implement serializable cannot save or restore their state.
(reading data into a file with IO stream is called serialization)
★ Object Graph
When an object is serialized, only the data of the object is saved, and the methods and constructors do not belong to the serialized stream. If a data variable is an object, the data members of the object are also serialized. The structure of the tree or object data, including these sub-objects, forms the object graph.
★ Instantaneous Transient
Prevents the object's properties from being serialized.
Demo Transient:
Packageio.sreializable;Importjava.io.Serializable;/** * Object class * @author Chen Haoxiang * * 2016-4-22 * * Public class Address implements Serializable{//implementation of SERIALIZABLE interfaces the//static variables are not serialized. For non-static variables, it is generally serialized, but not if declared as a transient type. Private StaticString address;//Static variables do not belong to the object! transient intNum//Instantaneous variable---the variable is not serialized---does not appear in the object graph PrivateString Name;//Name Private intAge//Age PrivateString Tel;//Telephone Public Address(intNum, String name,intAge, String Tel) { This. num = num; name = name; This. Age = Age; This. Tel = Tel; } Public StaticStringgetaddress() {returnAddress } Public Static void setaddress(String address) {address.address = Address; } Public int Getnum() {returnNum } Public void Setnum(intNUM) { This. num = num; } PublicStringGetName() {returnName; } Public void SetName(String name) {name = name; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } PublicStringGettel() {returnTel } Public void Settel(String Tel) { This. Tel = Tel; }@Override Public int hashcode() {Final intPrime = to;intresult =1; result = Prime * result + ((Name = =NULL) ?0: Name.hashcode ()); result = Prime * result + age; result = Prime * result + ((tel = =NULL) ?0: Tel.hashcode ());returnResult }@Override Public Boolean equals(Object obj) {if( This= = obj)return true;if(obj = =NULL)return false;if(GetClass ()! = Obj.getclass ())return false; Address other = (address) obj;if(Name = =NULL) {if(Other. Name! =NULL)return false; }Else if(! Name.equals (Other. Name))return false;if(age! = other.age)return false;if(tel = =NULL) {if(Other.tel! =NULL)return false; }Else if(!tel.equals (Other.tel))return false;return true; }@Override PublicStringtoString() {return "Address [num="+ num +", name="+ Name +", age="+ Age +", tel="+ Tel +"]"; }}
Package IO. Sreializable;Import Java. IO. FileInputStream;Import Java. IO. FileNotFoundException;Import Java. IO. FileOutputStream;Import Java. IO. IOException;Import Java. IO. ObjectInputStream;Import Java. IO. ObjectOutputStream;/** * * @author Chen Haoxiang * * 2016-4-22 * *public class Serializable {//Throw an exception directly ... Do project don't do this public static void main (string[] args) throws FileNotFoundException, IOException, classnotfoundexception { Address A1 = new address (1,"AAA", at,"12345678911");Address A2 = new address (2,"BBB", -,"12345678912");Address a3 = new address (3,"CCC", -,"12345678913");Address a4 = new address (4,"DDD", -,"12345678914");Address A5 = new address (5,"Eee", -,"12345678915");Object serialization---output, write ObjectOutputStream out= new ObjectOutputStream (New FileOutputStream ("Hx.txt")); out. WriteObject(A1); out. WriteObject(A2); out. WriteObject(A3); out. WriteObject(A4); out. WriteObject(A5); out. Close();Deserialization----Read ObjectInputStreaminch= new ObjectInputStream (New FileInputStream ("Hx.txt"));System. out. println(inch. ReadObject());System. out. println(inch. ReadObject());System. out. println(inch. ReadObject());System. out. println(inch. ReadObject());System. out. println(inch. ReadObject());If the object is read and the file is read, the EOF exception is thrown Java. IO. Eofexception}}
To run a picture:
As you can see, the value of the instantaneous variable is not stored, that is, not serialized!!
Buffered input/output stream Description:
(Bufferedinputstream and Bufferedoutputstream)
The first way:
innew DataInputStream( new BufferedInputStream( new FileInputStream("Test.txt"));
The second way:
innew DataInputStream( new FileInputStream("Test.txt"));
The Third Way:
innew BufferedInputStream( new DataInputStream( new FileInputStream("Test.java"));
Let's look at a comparison of how they run faster:
Because Bufferedinputstream does not read one line at a time, it can read only bytes,
So in the file, I'm storing a string of 97 bytes in a row.
I let it read 97 bytes at a time, so it is almost the same as the other 2 reading one line at a time.
This is the size of the file when you save a line of strings:
The following string, each line is the same size as the first line!
The file is altogether 112KB.
To run the code:
PackageIo.buffer;ImportJava.io.BufferedInputStream;ImportJava.io.DataInputStream;ImportJava.io.FileInputStream;ImportJava.io.FileNotFoundException;/** * Test 3 method Stream Read speed * @author Chen Haoxiang * * 2016-4-22 * * Public class bufferedstreamdemo { //Here, we simplify, throw the anomaly directly. Public Static void Main(string[] args)throwsException {//test1 (); //test2 ();Test3 (); }/** * First new buffer stream * @throws Exception */ Private Static void test3()throwsException {Longtime1 = System.currenttimemillis (); Bufferedinputstream in =NewBufferedinputstream (NewDataInputStream (NewFileInputStream ("Chx.txt")));byteB[] =New byte[ the];intn =0; while((N=in.read (b))!=-1) {System.out.println (NewString (b,0, n)); }LongTime2 = System.currenttimemillis (); System.out.println ((time2-time1) +"milliseconds");//test program run time requires multiple runs averaging}/** * No buffer stream * @throws Exception */ Private Static void test2()throwsException {Longtime1 = System.currenttimemillis (); DataInputStream in =NewDataInputStream (NewFileInputStream ("Chx.txt")); String str =NULL; while((Str=in.readline ())! =NULL) {System.out.println (str); }LongTime2 = System.currenttimemillis (); System.out.println ((time2-time1) +"milliseconds");//test program run time requires multiple runs averaging}/** * In the middle new buffer stream * @throws Exception */ Private Static void test1()throwsException {Longtime1 = System.currenttimemillis (); DataInputStream in =NewDataInputStream (NewBufferedinputstream (NewFileInputStream ("Chx.txt"))); String str =NULL; while((Str=in.readline ())! =NULL) {System.out.println (str); }LongTime2 = System.currenttimemillis (); System.out.println ((time2-time1) +"milliseconds");//test program run time requires multiple runs averaging}}
Operation Result:
I'm taking multiple runs, closest to the average!
The first way to run the result:
The second way after the result is run:
The third way after the result is run:
☆ Sample Test Technology Summary:
Can clearly see, the first way to use the shortest time!!!!
The second way is slowest, compared to the first method, 8 times times slower!!!!
Scenario 1 is the optimal
1) There is no faster than buffer;
2) buffer placed in the middle layer package is faster than on the outer layer;
3) faster by row or block operation than by Byte or character (operation with object flow is faster than Byte character)
4) The buffer to be combined with the flow can be used, on the basis of the flow of the convection function is enhanced.
Java---io enhancement (1)