The usage of random streams is similar to that of other languages (C/PHP). Therefore, it is used in the end and has powerful functions.
| The code is as follows: |
Copy code |
RandomAccessFile objectOut = new RandomAccessFile (accountNumber + "_info.txt", "rw "); |
A bunch of read and write operations are provided. By default, they are from the beginning. If you want to change the position, use seek (length). You can use the built-in length () method to obtain the length, in this way, the final position can be located.
The effects of readLine () and readUTF () are similar, but different. readLine () is not supported for Chinese. If you need readLine to read Chinese, you may need to output this output.
| The code is as follows: |
Copy code |
String str = objectOut. readLine (); Byte B [] = str. getBytes ("ISO-8859-1 "); |
However, readUTF () can be used directly, and writeUTF () can be used for writing.
In addition, there is no need to break the line too much. Each read operation corresponds to each write operation, and the current string is located at the end of each read operation. Therefore, you only need to write the statement as follows:
| The code is as follows: |
Copy code |
ObjectOut. writeUTF (String. valueOf (accountNumber )); ObjectOut. writeUTF (accountName ); ObjectOut. writeDouble (balance ); ObjectOut. writeUTF ("current account "); |
You can.
You can use getFilePointer () to obtain the current location.