From the previous study, we know that if you are going to read a file, you need to create an input stream pointing to the file; if you are going to write a file, you need to create an output stream pointing to the file. So, can you create a stream through which you can both read and write files? This is exactly what this section is about random streams.
The stream created by the RandomAccessFile class is called a random stream . Unlike the previous input and output streams, the RandomAccessFile class is neither a subclass of the InputStream class nor a subclass of the OutputStream class. However, the point of the stream created by the RandomAccessFile class can be used as the source of the stream, or as the destination of the stream. In other words, when preparing to read and write a file, create a random stream pointing to the file, so You can read data from the file from this stream, and you can also write data to the file through this stream.
The following are the two constructors of the RandomAccessFile class:
The name parameter is used to determine a file name, giving the source of the created stream, which is also the stream destination. The parameter mode is r (read-only) or rw (read-write), which determines the access rights of the created stream to the file.
The parameter file is a File object that gives the source of the created stream and is also the stream destination. The parameter mode is r (read-only) or rw (read-write), which determines the access rights of the created stream to the file.
Note : When the RandomAccessFile stream points to a file, the file is not refreshed.
There is a method seek(long a) in the RandomAccessFile class that is used to locate the read and write position of the RandomAccessFile stream. The parameter a determines the number of bytes from the read and write position to the beginning of the file. In addition, the stream can also call the getFilePointer() method to obtain the current read and write position of the stream. The RandomAccessFile stream is more flexible for reading and writing files than sequential reading and writing.
For example, write several int integers into a file named tom.dat, and then read the data in reverse order:
importjava.io.*;publicclassMain{publicstaticvoidmain(Stringargs[]){RandomAccessFileinAndOut=null;intdata[]={1,2,3,4,5,6,7,8,9,10};try{inAndOut=newRandomAccessFile (tom.dat,rw);for(inti=0;i<data.length;i++){inAndOut.writeInt(data[i]);}for(longi=data.length-1;i>=0;i --){inAndOut.seek(i*4);System.out.printf(t%d,inAndOut.readInt());/*An int type data occupies 4 bytes, inAndOut starts from the 36th of the file Read the last integer bytes, and read an integer every 4 bytes forward*/}inAndOut.close();}catch(IOExceptione){}}}
Common methods of the RandomAccessFile stream are as follows:
Note : When the readLine() method of the RandomAccessFile stream reads files containing non-ASCⅡ characters, such as files containing Chinese characters, "garbled characters" will appear. Therefore, it is necessary to re-encode the string read by readLine() using "iso-8859-1" encoding and store it in a byte array, and then use the default encoding of the current machine to convert the array into a string. The operation is as follows:
Stringstr=in.readLine();
byteb[]=str.getBytes(iso-8859-1);
Stringcontent=newString(b);
If the default encoding of the machine is "GB2312", then
Stringcontent=newString(b);
Equivalent to
Stringcontent=newString(b,GB2312);
For example:
importjava.io.*;publicclassMain{publicstaticvoidmain(Stringargs[]){RandomAccessFilein=null;try{in=newRandomAccessFile(Main.java,rw);longlength=in.length();//Get the length of the file longposition=0; in.seek(position);//Locate the reading position to the beginning of the file while(position<length){Stringstr=in.readLine();byteb[]=str.getBytes(iso-8859-1);str =newString(b);position=in.getFilePointer();System.out.println(str);}}catch(IOExceptione){}}}