There are two ways to access files: sequential access and random access.
Sequential access is access that reads the file in order from the beginning, and random access is access that specifies a specific byte position of the file.
Sequential access is used when dealing with variable length text files, and random access is used when processing fixed length binary files at high speed.
What is sequential access?
Sequential access is also known as sequential access. Sequential access is an access method that reads files in order from the beginning. Open the file with open function and read it line by line with readline function It will be sequential access. Text files are accessed this way.
The downside of sequential access is that, for example, if the part you want to change is on line 10,000 of the file, you need to read 9999 lines that you don't need. Sequential access is not suitable when you need fast access.
However, when dealing with variable-length text files that use a line break as delimiters, only sequential access can be used.
What is random access?
Random access is a file access method that is performed by specifying a specific byte position in the file. For example, specify "10th to 8th bytes". It's called Random Access, but it doesn't mean random access. Means access with a specified byte position.
| - -- -- -- -- -- -- -- -- -- -- -- -- - | | | | Data | | | | - -- + - -- -- - + - -- - + - -- --- + - -- --- |
If the above "data" is the 10th byte from the beginning and 8 bytes of data, you can access it directly by specifying the position.
You can specify the byte position directly, which makes accessing the file very fast. This is because there is no need to read extra parts like sequential access. However, in order to access a file by specifying a specific byte position, the file must be designed to handle fixed-length data. If the file is variable in length, it cannot point to a specific location.
Comparison of sequential access and random access
|Twice||Sequential access||Random access|
|Access method||Sequential reading||Specifying the byte position|
|Main uses||Reading a text file||Applications that require speed|
|speed||The slower the data is||High speed due to direct access|
|File design||Variable length data is also possible||Fixed length data|